Index: branches/tap_branches/psastro/src/psastroMosaicAstrom.c
===================================================================
--- branches/tap_branches/psastro/src/psastroMosaicAstrom.c	(revision 25900)
+++ branches/tap_branches/psastro/src/psastroMosaicAstrom.c	(revision 27838)
@@ -1,5 +1,5 @@
 /** @file psastroMosaicAstrom.c
  *
- *  @brief 
+ *  @brief
  *
  *  @ingroup libpsastro
@@ -20,5 +20,4 @@
 
     bool status;
-    char filename[256];
 
     // select the current recipe
@@ -38,37 +37,30 @@
     pmFPA *fpa = input->fpa;
 
-    // before we do object matches, we need to (optionally) fix failed chips.  We compare chips with
-    // the supplied mosaic model.  Adjust significant outliers to match model.
-    # if (0)
-    if (!psastroFixChips (config, recipe)) {
-        psError(PSASTRO_ERR_UNKNOWN, false, "failed to align problematic chips");
-        return false;
-    }
-    if (!psastroFixChipsTest (config, recipe)) {
-        psError(PSASTRO_ERR_UNKNOWN, false, "failed to align problematic chips");
-        return false;
-    }
-    # endif
-
     char *outroot = psMetadataLookupStr (&status, config->arguments, "OUTPUT");
     if (!status || !outroot) psAbort ("Can't find outroot on config->arguments");
 
-    if (!psastroMosaicFit (fpa, recipe, outroot, 0)) return false;
-    if (!psastroMosaicFit (fpa, recipe, outroot, 1)) return false;
-    if (!psastroMosaicFit (fpa, recipe, outroot, 2)) return false;
-    if (!psastroMosaicFit (fpa, recipe, outroot, 3)) return false;
+    int nIter = psMetadataLookupS32 (&status, recipe, "PSASTRO.MOSAIC.CHIP.NITER");
+    if (!status) psAbort ("missing config value");
+
+    // this should be in a loop with nIter =
+    for (int iter = 0; iter < nIter; iter++) {
+        if (!psastroMosaicFit (fpa, recipe, outroot, iter)) return false;
+    }
 
     // now fit the chips under the common distortion with higher-order terms
     // first, re-perform the match with a slightly tighter circle
-    if (!psastroMosaicSetMatch (fpa, recipe, 4)) {
+    if (!psastroMosaicSetMatch (fpa, recipe, nIter)) {
         psError(PSASTRO_ERR_UNKNOWN, false, "failed to match raw and ref stars for mosaic (4th pass)");
         return false;
     }
-    if (!psastroMosaicChipAstrom (fpa, recipe, 4)) {
+    if (!psastroMosaicChipAstrom (fpa, recipe, nIter)) {
         psError(PSASTRO_ERR_UNKNOWN, false, "failed to measure chip astrometry in mosaic mode (4th pass)");
         return false;
     }
+
     if (psTraceGetLevel("psastro.dump") > 0) {
-        snprintf (filename, 256, "%s.10.dat", outroot);
+        // the last filename (see filenames in psastroMosaicFit)
+        char filename[256];
+        snprintf (filename, 256, "%s.%d.dat", outroot, 2*nIter + 2);
         psastroDumpMatches (fpa, filename);
     }
@@ -83,5 +75,6 @@
     }
     if (!pmAstromWriteBilevelMosaic (updates, fpa, NONLIN_TOL)) {
-        psAbort ("failed to save header terms");
+        psError(psErrorCodeLast(), false, "Failed to save header terms");
+        return false;
     }
 
