diff --git a/ChangeLog b/ChangeLog
index 3ce987daab0ad0591d5f9f1cbf21b160c63c5308..748a61d47188e59b40190267bd97f5eeb95dc0ca 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -4,6 +4,7 @@
 	two-column characters properly.  This fixes Savannah bug #31743.
 	* src/nano.c (precalc_multicolorinfo): Improve debugging messages,
 	and remove superfluous assignment (fileptr already equals endptr).
+	* src/color.c (color_update): Move magic check to after headerline.
 
 2014-05-13  Benno Schulenberg  <bensberg@justemail.net>
 	* src/text.c (do_linter): Make an error message somewhat clearer.
diff --git a/src/color.c b/src/color.c
index f064fae1fefd02dc00f6ff682f437f108531683b..af4261d2922e75fd64d1270017bf25ff381f31b5 100644
--- a/src/color.c
+++ b/src/color.c
@@ -249,25 +249,27 @@ void color_update(void)
 	    }
 	}
 
-#ifdef HAVE_LIBMAGIC
-	/* Check magic if we don't have an answer yet. */
+	/* Check the headerline if the extension didn't match anything. */
 	if (openfile->colorstrings == NULL) {
 #ifdef DEBUG
-	    fprintf(stderr, "No match using extension, trying libmagic...\n");
+	    fprintf(stderr, "No result from file extension, trying headerline...\n");
 #endif
 	    for (tmpsyntax = syntaxes; tmpsyntax != NULL;
 		tmpsyntax = tmpsyntax->next) {
-		for (e = tmpsyntax->magics; e != NULL; e = e->next) {
+
+		for (e = tmpsyntax->headers; e != NULL; e = e->next) {
 		    bool not_compiled = (e->ext == NULL);
+
 		    if (not_compiled) {
 			e->ext = (regex_t *)nmalloc(sizeof(regex_t));
 			regcomp(e->ext, fixbounds(e->ext_regex), REG_EXTENDED);
 		    }
 #ifdef DEBUG
-		    fprintf(stderr, "Matching regex \"%s\" against \"%s\"\n", e->ext_regex, magicstring);
+		    fprintf(stderr, "Comparing header regex \"%s\" to fileage \"%s\"...\n",
+				    e->ext_regex, openfile->fileage->data);
 #endif
-		    /* Set colorstrings if we match the magic-string regex. */
-		    if (magicstring && regexec(e->ext, magicstring, 0, NULL, 0) == 0) {
+		    /* Set colorstrings if we match the header-line regex. */
+		    if (regexec(e->ext, openfile->fileage->data, 0, NULL, 0) == 0) {
 			openfile->syntax = tmpsyntax;
 			openfile->colorstrings = tmpsyntax->color;
 			break;
@@ -278,17 +280,17 @@ void color_update(void)
 		}
 	    }
 	}
-#endif /* HAVE_LIBMAGIC */
 
-	/* If we haven't matched anything yet, try the headers. */
+#ifdef HAVE_LIBMAGIC
+	/* Check magic if we don't have an answer yet. */
 	if (openfile->colorstrings == NULL) {
 #ifdef DEBUG
-	    fprintf(stderr, "No match for file extensions, looking at headers...\n");
+	    fprintf(stderr, "No result from headerline either, trying libmagic...\n");
 #endif
 	    for (tmpsyntax = syntaxes; tmpsyntax != NULL;
 		tmpsyntax = tmpsyntax->next) {
 
-		for (e = tmpsyntax->headers; e != NULL; e = e->next) {
+		for (e = tmpsyntax->magics; e != NULL; e = e->next) {
 		    bool not_compiled = (e->ext == NULL);
 
 		    if (not_compiled) {
@@ -296,11 +298,10 @@ void color_update(void)
 			regcomp(e->ext, fixbounds(e->ext_regex), REG_EXTENDED);
 		    }
 #ifdef DEBUG
-		    fprintf(stderr, "Comparing header regex \"%s\" to fileage \"%s\"...\n",
-				    e->ext_regex, openfile->fileage->data);
+		    fprintf(stderr, "Matching regex \"%s\" against \"%s\"\n", e->ext_regex, magicstring);
 #endif
-		    /* Set colorstrings if we match the header-line regex. */
-		    if (regexec(e->ext, openfile->fileage->data, 0, NULL, 0) == 0) {
+		    /* Set colorstrings if we match the magic-string regex. */
+		    if (magicstring && regexec(e->ext, magicstring, 0, NULL, 0) == 0) {
 			openfile->syntax = tmpsyntax;
 			openfile->colorstrings = tmpsyntax->color;
 			break;
@@ -311,6 +312,7 @@ void color_update(void)
 		}
 	    }
 	}
+#endif /* HAVE_LIBMAGIC */
     }
 
     /* If we didn't find any syntax yet, and we do have a default one,