diff --git a/sieve/tests/matches.sieve b/sieve/tests/matches.sieve
index 35552bb1d1ade6b381dfeb3dc1ad0d4a61ada72f..b381f030ab429d0603f68bc6b135884dd49ae30d 100644
--- a/sieve/tests/matches.sieve
+++ b/sieve/tests/matches.sieve
@@ -37,36 +37,36 @@ if header :matches "x-bullshit" "*3333\\?\\?\\?a" {
 
 fileinto "SHOULD NOT MATCH";
 if address :matches "from" "*@d*kn*ers.com" {
-	fileinto "A";
+	fileinto "NA";
 }
 if address :matches "from" "stepan+sieve@drunksnipers.*" {
-	fileinto "B";
+	fileinto "NB";
 }
 if address :matches "from" "*+sieve@drunksnipers.om" {
-	fileinto "C";
+	fileinto "NC";
 }
 if address :matches "from" "stephan+sieve?drunksipers.com" {
-	fileinto "D";
+	fileinto "ND";
 }
 if address :matches "from" "?tephan+sievedrunksnipers.com" {
-	fileinto "E";
+	fileinto "NE";
 }
 if address :matches "from" "sephan+sieve@drunksnipers.co?" {
-	fileinto "F";
+	fileinto "NF";
 }
 if address :matches "from" "?t?phan?sieve?dunksnip?rs.co?" {
-	fileinto "G";
+	fileinto "NG";
 }
 if header :matches "x-bullshit" "33333\\?\\?\\?" {
-	fileinto "H";
+	fileinto "NH";
 }
 if header :matches "x-bullshit" "33333\\?\\?\\?aa" {
-    fileinto "I";
+    fileinto "NI";
 }
 if header :matches "x-bullshit" "\\*3333\\?\\?\\?a" {
-    fileinto "J";
+    fileinto "NJ";
 }
 if header :matches "x-bullshit" "\\f3333\\?\\?\\?a" {
-    fileinto "J";
+    fileinto "NK";
 }
 
diff --git a/src/lib-sieve/mcht-matches.c b/src/lib-sieve/mcht-matches.c
index 18da6ee2d13e9f207489b962c9f36e6e87105b3f..4ab7b75e3140b129a144c05f98b019651d6c1838 100644
--- a/src/lib-sieve/mcht-matches.c
+++ b/src/lib-sieve/mcht-matches.c
@@ -38,7 +38,7 @@ const struct sieve_match_type matches_match_type = {
  */
 
 /* Quick 'n dirty debug */
-//#define MATCH_DEBUG
+#define MATCH_DEBUG
 #ifdef MATCH_DEBUG
 #define debug_printf(...) printf (__VA_ARGS__)
 #else
@@ -225,7 +225,6 @@ static bool mcht_matches_match
 				str_append_n(mvalue, pvp, qp-pvp);
 				for ( ; qp < qend; qp++ )
 					str_append_c(mchars, *qp);
-					//sieve_match_values_add_char(mvalues, *qp); 
 				debug_printf("MATCH :: %s\n", str_c(mvalue));
 			}
 			
@@ -249,7 +248,8 @@ static bool mcht_matches_match
 				debug_printf("MATCH fkey: '%s'\n", t_strdup_until(needle, nend));
 				debug_printf("MATCH fval: '%s'\n", t_strdup_until(vp, vend));
 
-				if ( !cmp->char_match(cmp, &vp, vend, &needle, nend) ) {	
+				if ( (needle == nend && vp < vend ) || 
+					!cmp->char_match(cmp, &vp, vend, &needle, nend) ) {	
 					if ( prv != NULL && prv + 1 < vend ) {
 						vp = prv;
 						kp = prk;
@@ -262,7 +262,12 @@ static bool mcht_matches_match
 						next_wcard = '?';
 				
 						backtrack = TRUE;				 
+						debug_printf("MATCH backtrack\n");
+					} else {
+						/* We are sure to have failed */
+						return FALSE;
 					}
+					
 					debug_printf("MATCH failed fixed\n");
 					break;
 				}
diff --git a/src/lib-sieve/tst-header.c b/src/lib-sieve/tst-header.c
index fc5e2e19f0406c43d201a3accab0fbe152f471a3..6c05ee4f7b4a21f4d5a8611076e73f9ed5679b64 100644
--- a/src/lib-sieve/tst-header.c
+++ b/src/lib-sieve/tst-header.c
@@ -213,11 +213,15 @@ static bool tst_header_operation_execute
 	while ( !matched && (result=sieve_coded_stringlist_next_item(hdr_list, &hdr_item)) 
 		&& hdr_item != NULL ) {
 		const char *const *headers;
+
+		printf("HEADER: %s\n", str_c(hdr_item));
+
 			
 		if ( mail_get_headers_utf8(renv->msgdata->mail, str_c(hdr_item), &headers) >= 0 ) {	
 			
 			int i;
 			for ( i = 0; !matched && headers[i] != NULL; i++ ) {
+				printf("HEADER: %s %s\n", str_c(hdr_item), headers[i]);
 				if ( sieve_match_value(mctx, headers[i], strlen(headers[i])) )
 					matched = TRUE;				
 			} 
diff --git a/src/testsuite/tests/match-types/matches.svtest b/src/testsuite/tests/match-types/matches.svtest
index 8b613c531a20c2bf42bda4e2e855ade80dcc5c6b..f543739b1fb5888b2d6f1506e3af18201edf810a 100644
--- a/src/testsuite/tests/match-types/matches.svtest
+++ b/src/testsuite/tests/match-types/matches.svtest
@@ -71,7 +71,7 @@ test "MATCH-I" {
 }
 
 test "MATCH-J" {
-	if not header :matches "x-bullshit" "*\\?\\?\\?a" {
+	if not header :matches "X-Bullshit" "*\\?\\?\\?a" {
 		test_fail "J case should have matched";
 	}
 }