Index: /branches/eam_branches/relphot.20210521/src/ZeroPointModes.c
===================================================================
--- /branches/eam_branches/relphot.20210521/src/ZeroPointModes.c	(revision 41630)
+++ /branches/eam_branches/relphot.20210521/src/ZeroPointModes.c	(revision 41631)
@@ -104,10 +104,10 @@
 
   if (!TGROUP_ZEROPT && MOSAIC_ZEROPT) {
-    if (CurrentLoop <= 4) {
+    if (CurrentLoop < 6) {
       TGROUP_ZPT_MODE = TGROUP_ZPT_MODE_NONE;
       MOSAIC_ZPT_MODE = MOSAIC_ZPT_MODE_ALL;
       IMAGE_ZPT_MODE  = IMAGE_ZPT_MODE_NONE;
     }
-    if ((CurrentLoop > 4) && (CurrentLoop <= 999)) {
+    if ((CurrentLoop >= 6) && (CurrentLoop < 12)) {
       // after iterating a few times on the Mosaic zero points:
       // * identify the good / bad mosaics
@@ -118,4 +118,27 @@
       clean_mosaics(); // do this on every pass or just sometimes?
     }
+    if ((CurrentLoop >= 12) && (CurrentLoop < 18)) {
+      // after iterating a few times on the Mosaic zero points:
+      // * identify the good / bad mosaics
+      // * fit the images from the bad mosaics
+      TGROUP_ZPT_MODE = TGROUP_ZPT_MODE_NONE; // stop fitting the bad nights
+      MOSAIC_ZPT_MODE = MOSAIC_ZPT_MODE_GOOD_MOSAIC;
+      IMAGE_ZPT_MODE  = IMAGE_ZPT_MODE_BAD_MOSAIC;
+      clean_mosaics(); // do this on every pass or just sometimes?
+      clean_images(); // do this on every pass 
+      clean_stars(catalog, Ncatalog); // do this on every pass 
+    }
+    if ((CurrentLoop >= 18) && (CurrentLoop < 999)) {
+      // after iterating a few times on the Mosaic zero points:
+      // * identify the good / bad mosaics
+      // * fit the images from the bad mosaics
+      TGROUP_ZPT_MODE = TGROUP_ZPT_MODE_NONE; // stop fitting the bad nights
+      MOSAIC_ZPT_MODE = MOSAIC_ZPT_MODE_GOOD_MOSAIC;
+      IMAGE_ZPT_MODE  = IMAGE_ZPT_MODE_BAD_MOSAIC;
+      GRID_ZPT_MODE  = GRID_ZPT_MODE_ALL; // only fit bad mosaics on bad nights
+      clean_mosaics(); // do this on every pass or just sometimes?
+      clean_images(); // do this on every pass 
+      clean_stars(catalog, Ncatalog); // do this on every pass 
+    }
   }
 
Index: /branches/eam_branches/relphot.20210521/src/relphot_images.c
===================================================================
--- /branches/eam_branches/relphot.20210521/src/relphot_images.c	(revision 41630)
+++ /branches/eam_branches/relphot.20210521/src/relphot_images.c	(revision 41631)
@@ -166,12 +166,19 @@
   // be used.
   freeMosaics ();
+  freeTGroups ();
   MOSAIC_ZEROPT = FALSE;
   TGROUP_ZEROPT = FALSE;
 
+  // only change the real database files if -update is requested
+  if (!UPDATE) {
+    dvo_image_unlock (&db); 
+    freeImages (db.ftable.buffer);
+    gfits_db_free (&db);
+    freeGridBins ();
+    return TRUE;
+  }
+  
   reload_images (&db);
 
-  // only change the real database files if -update is requested
-  if (!UPDATE) exit (0);
-  
   /* Load catalog data from region files, update Mrel include all data.  In a parallel
      context, this function writes the image parameters as a subset table for the remote
