diff --git a/ChangeLog b/ChangeLog
index 14734fc9cd9e3c2d766d96a896dba8e17d288a1a..7aeca7a105b3274bd8959a020838381e7dda6445 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -49,10 +49,11 @@ CVS code -
 	  any variables used to hold them shorts.  Changes to
 	  do_colorinit() (renamed color_init()), color_to_int() (renamed
 	  color_to_short()), and parse_colors(). (DLR)
-	- Change color handling to save only the regex strings
-	  constantly,  and to actually compile them on an as-needed
-	  basis. Changes to update_color() and
-	  thanks_for_all_the_fish(). (Brand Huntsman and DLR)
+	- Change color handling to save only the extension and color
+	  regex strings constantly,  and to actually compile them on an
+	  as-needed basis. Changes to update_color(),
+	  thanks_for_all_the_fish(), nregcomp(), parse_syntax(), and
+	  parse_colors(). (Brand Huntsman and DLR)
 	- Various other color fixes.  Handle unspecified foreground
 	  colors properly, don't automatically reinitialize the
 	  displayed colors every time we update the current buffer's
diff --git a/src/color.c b/src/color.c
index 547ba1c6236be04330ec8e013d6396d6622a3dd0..4d9b1938b5000a7ad05e0eba11e7cfbaa62b3ea1 100644
--- a/src/color.c
+++ b/src/color.c
@@ -115,11 +115,19 @@ void color_update(void)
     openfile->colorstrings = NULL;
     for (tmpsyntax = syntaxes; tmpsyntax != NULL;
 	tmpsyntax = tmpsyntax->next) {
-	const exttype *e;
+	exttype *e;
 
 	for (e = tmpsyntax->extensions; e != NULL; e = e->next) {
+	    /* e->ext_regex has already been checked for validity
+	     * elsewhere.  Compile its specified regex if we haven't
+	     * already. */
+	    if (e->ext == NULL) {
+		e->ext = (regex_t *)nmalloc(sizeof(regex_t));
+		regcomp(e->ext, e->ext_regex, REG_EXTENDED);
+	    }
+
 	    /* Set colorstrings if we matched the extension regex. */
-	    if (regexec(&e->ext, openfile->filename, 0, NULL, 0) == 0)
+	    if (regexec(e->ext, openfile->filename, 0, NULL, 0) == 0)
 		openfile->colorstrings = tmpsyntax->color;
 
 	    if (openfile->colorstrings != NULL)
@@ -139,18 +147,18 @@ void color_update(void)
 	}
     }
 
-    /* tmpcolor->start_regex and tmpcolor->end_regex have already been
-     * checked for validity elsewhere.  Compile their associated regexes
-     * if we haven't already. */
     for (tmpcolor = openfile->colorstrings; tmpcolor != NULL;
 	tmpcolor = tmpcolor->next) {
-	if (tmpcolor->start_regex != NULL) {
+	/* tmpcolor->start_regex and tmpcolor->end_regex have already
+	 * been checked for validity elsewhere.  Compile their specified
+	 * regexes if we haven't already. */
+	if (tmpcolor->start == NULL) {
 	    tmpcolor->start = (regex_t *)nmalloc(sizeof(regex_t));
 	    regcomp(tmpcolor->start, tmpcolor->start_regex,
 		REG_EXTENDED | (tmpcolor->icase ? REG_ICASE : 0));
 	}
 
-	if (tmpcolor->end_regex != NULL) {
+	if (tmpcolor->end_regex != NULL && tmpcolor->end == NULL) {
 	    tmpcolor->end = (regex_t *)nmalloc(sizeof(regex_t));
 	    regcomp(tmpcolor->end, tmpcolor->end_regex,
 		REG_EXTENDED | (tmpcolor->icase ? REG_ICASE : 0));
diff --git a/src/global.c b/src/global.c
index ee416f566927bac9b22d4ebefa7cd8661d422882..0617728a65f52ad365ace39b229af72c0645f862 100644
--- a/src/global.c
+++ b/src/global.c
@@ -1219,15 +1219,16 @@ void thanks_for_all_the_fish(void)
 	    exttype *bob = syntaxes->extensions;
 
 	    syntaxes->extensions = bob->next;
-	    regfree(&bob->ext);
+	    free(bob->ext_regex);
+	    regfree(bob->ext);
+	    free(bob->ext);
 	    free(bob);
 	}
 	while (syntaxes->color != NULL) {
 	    colortype *bob = syntaxes->color;
 
 	    syntaxes->color = bob->next;
-	    if (bob->start_regex != NULL)
-		free(bob->start_regex);
+	    free(bob->start_regex);
 	    if (bob->start != NULL) {
 		regfree(bob->start);
 		free(bob->start);
diff --git a/src/nano.h b/src/nano.h
index e250b3dc5560ccfbf8564a316beccfd5765bf3a1..9a33d264359645562875cdf96385a854735891b7 100644
--- a/src/nano.h
+++ b/src/nano.h
@@ -195,7 +195,8 @@ typedef struct colortype {
 } colortype;
 
 typedef struct exttype {
-    regex_t ext;		/* The extensions that match this
+    char *ext_regex;		/* Extensions that match this syntax. */
+    regex_t *ext;		/* Compiled extensions that match this
 				 * syntax. */
     struct exttype *next;
 } exttype;
diff --git a/src/proto.h b/src/proto.h
index 75a37fd557feb4f00b8b5bd40e01b04b542cdc47..3cfae3b52e07b2db7d7736a7878b8058c1989f6e 100644
--- a/src/proto.h
+++ b/src/proto.h
@@ -417,7 +417,7 @@ char *parse_argument(char *ptr);
 #ifdef ENABLE_COLOR
 short color_to_short(const char *colorname, bool *bright);
 char *parse_next_regex(char *ptr);
-bool nregcomp(regex_t *preg, const char *regex, int eflags);
+bool nregcomp(const char *regex, int eflags);
 void parse_syntax(char *ptr);
 void parse_colors(char *ptr, bool icase);
 #endif /* ENABLE_COLOR */
diff --git a/src/rcfile.c b/src/rcfile.c
index 929ef79f2086a8f52b969527d7aa3fb025e9e9a7..95277913a9066bb4591d2096d1d52b3065af1dc5 100644
--- a/src/rcfile.c
+++ b/src/rcfile.c
@@ -244,21 +244,24 @@ char *parse_next_regex(char *ptr)
     return ptr;
 }
 
-/* Compile the regular expression regex to preg.  Return TRUE on
- * success, or FALSE if the expression is invalid. */
-bool nregcomp(regex_t *preg, const char *regex, int eflags)
+/* Compile the regular expression regex to see if it's valid.  Return
+ * TRUE if it is, or FALSE otherwise. */
+bool nregcomp(const char *regex, int eflags)
 {
-    int rc = regcomp(preg, regex, REG_EXTENDED | eflags);
+    regex_t preg;
+    int rc = regcomp(&preg, regex, REG_EXTENDED | eflags);
 
     if (rc != 0) {
-	size_t len = regerror(rc, preg, NULL, 0);
+	size_t len = regerror(rc, &preg, NULL, 0);
 	char *str = charalloc(len);
 
-	regerror(rc, preg, str, len);
+	regerror(rc, &preg, str, len);
 	rcfile_error(N_("Bad regex \"%s\": %s"), regex, str);
 	free(str);
     }
 
+    regfree(&preg);
+
     return (rc == 0);
 }
 
@@ -299,11 +302,13 @@ void parse_syntax(char *ptr)
 	fprintf(stderr, "Adding new syntax after first one\n");
 #endif
     }
+
     endsyntax->desc = mallocstrcpy(NULL, nameptr);
     endsyntax->color = NULL;
     endcolor = NULL;
     endsyntax->extensions = NULL;
     endsyntax->next = NULL;
+
 #ifdef DEBUG
     fprintf(stderr, "Starting a new syntax type: \"%s\"\n", nameptr);
 #endif
@@ -327,7 +332,12 @@ void parse_syntax(char *ptr)
 	    break;
 
 	newext = (exttype *)nmalloc(sizeof(exttype));
-	if (nregcomp(&newext->ext, fileregptr, REG_NOSUB)) {
+
+	/* Save the extension regex if it's valid. */
+	if (nregcomp(fileregptr, REG_NOSUB)) {
+	    newext->ext_regex = mallocstrcpy(NULL, fileregptr);
+	    newext->ext = NULL;
+
 	    if (endext == NULL)
 		endsyntax->extensions = newext;
 	    else
@@ -422,28 +432,27 @@ void parse_colors(char *ptr, bool icase)
 
 	ptr++;
 
-	newcolor = (colortype *)nmalloc(sizeof(colortype));
 	fgstr = ptr;
 	ptr = parse_next_regex(ptr);
 	if (ptr == NULL)
 	    break;
 
-	newcolor->start = (regex_t *)nmalloc(sizeof(regex_t));
-	if (nregcomp(newcolor->start, fgstr, icase ? REG_ICASE : 0)) {
-	    /* Free this regex, now that we know it's valid, and save
-	     * the original string, so that we can recompile this regex
-	     * later as needed. */
-	    newcolor->start_regex = mallocstrcpy(NULL, fgstr);
-	    regfree(newcolor->start);
-	    free(newcolor->start);
-	    newcolor->start = NULL;
+	newcolor = (colortype *)nmalloc(sizeof(colortype));
 
+	/* Save the starting regex string if it's valid, and set up the
+	 * color information. */
+	if (nregcomp(fgstr, icase ? REG_ICASE : 0)) {
 	    newcolor->fg = fg;
 	    newcolor->bg = bg;
 	    newcolor->bright = bright;
 	    newcolor->icase = icase;
+
+	    newcolor->start_regex = mallocstrcpy(NULL, fgstr);
+	    newcolor->start = NULL;
+
 	    newcolor->end_regex = NULL;
 	    newcolor->end = NULL;
+
 	    newcolor->next = NULL;
 
 	    if (endcolor == NULL) {
@@ -457,9 +466,9 @@ void parse_colors(char *ptr, bool icase)
 #endif
 		endcolor->next = newcolor;
 	    }
+
 	    endcolor = newcolor;
 	} else {
-	    free(newcolor->start);
 	    free(newcolor);
 	    cancelled = TRUE;
 	}
@@ -489,17 +498,9 @@ void parse_colors(char *ptr, bool icase)
 	    if (cancelled)
 		continue;
 
-	    newcolor->end = (regex_t *)nmalloc(sizeof(regex_t));
-	    if (nregcomp(newcolor->end, fgstr, icase ? REG_ICASE : 0)) {
-		/* Free this regex, now that we know it's valid, and
-		 * save the original string, so that we can recompile
-		 * this regex later as needed. */
-		newcolor->end_regex = mallocstrcpy(NULL, fgstr);
-		regfree(newcolor->end);
-	    }
-
-	    free(newcolor->end);
-	    newcolor->end = NULL;
+	    /* Save the ending regex string if it's valid. */
+	    newcolor->end_regex = (nregcomp(fgstr, icase ? REG_ICASE :
+		0)) ? mallocstrcpy(NULL, fgstr) : NULL;
 	}
     }
 }