Index: /branches/eam_branches/ipp-pstamp-20260421/ppStack/src/ppStackCleanup.c
===================================================================
--- /branches/eam_branches/ipp-pstamp-20260421/ppStack/src/ppStackCleanup.c	(revision 43018)
+++ /branches/eam_branches/ipp-pstamp-20260421/ppStack/src/ppStackCleanup.c	(revision 43019)
@@ -141,4 +141,6 @@
     pmVisualCleanup ();
 
+    psLogMsg("ppStack", PS_LOG_INFO, "Memory leaks: %d\n", psMemCheckLeaks(0, NULL, stdout, false));
+
     exitValue = ppStackExitCode(exitValue);
     exit(exitValue);
Index: /branches/eam_branches/ipp-pstamp-20260421/ppStack/src/ppStackFiles.c
===================================================================
--- /branches/eam_branches/ipp-pstamp-20260421/ppStack/src/ppStackFiles.c	(revision 43018)
+++ /branches/eam_branches/ipp-pstamp-20260421/ppStack/src/ppStackFiles.c	(revision 43019)
@@ -253,4 +253,5 @@
 	    return false;
 	}
+	psFree(tmphead);
     }
     if (!psFitsClose(fits)) {
Index: /branches/eam_branches/ipp-pstamp-20260421/ppStack/src/ppStackOptions.c
===================================================================
--- /branches/eam_branches/ipp-pstamp-20260421/ppStack/src/ppStackOptions.c	(revision 43018)
+++ /branches/eam_branches/ipp-pstamp-20260421/ppStack/src/ppStackOptions.c	(revision 43019)
@@ -32,4 +32,8 @@
     psFree(options->inspect);
     psFree(options->rejected);
+    psFree(options->zpInput);
+    psFree(options->expTimeInput);
+    psFree(options->airmassInput);
+    psFree(options->bkgImages);
 
     return;
Index: /branches/eam_branches/ipp-pstamp-20260421/ppStack/src/ppStackSources.c
===================================================================
--- /branches/eam_branches/ipp-pstamp-20260421/ppStack/src/ppStackSources.c	(revision 43018)
+++ /branches/eam_branches/ipp-pstamp-20260421/ppStack/src/ppStackSources.c	(revision 43019)
@@ -295,5 +295,5 @@
     if (options->matchZPs) {
 
-      psVector *trans = pmSourceMatchRelphot(matches, matchedSources, zp, tol, iter1, starRej1, starSys1,
+	psVector *trans = pmSourceMatchRelphot(matches, matchedSources, zp, tol, iter1, starRej1, starSys1,
                                                iter2, starRej2, starSys2, starLimit,
                                                transIter, transRej, transThresh); // Transparencies per image
@@ -345,4 +345,5 @@
     dumpMatches("source_match2.dat", num, matches, zp, trans);
 #endif
+        // XXX this second block is always true (since options->matchZPs is true above)
         if (options->matchZPs) {
             options->norm = psVectorAlloc(num, PS_TYPE_F32);
@@ -383,5 +384,7 @@
             }
         }
-	matches = pmSourceMatchSources(sourceLists, radius, true); // List of matches
+
+	// XXX this duplicates the pmSourceMatch above
+	// matches = pmSourceMatchSources(sourceLists, radius, true); // List of matches
 
         if (zpExpNum == numGoodImages) {
@@ -393,5 +396,4 @@
             options->zp = NAN;
         }
-
 
         psFree(trans);
@@ -487,63 +489,69 @@
 
 
-    if (options->photometry) {
-        // Save best matches for future photometry
-      psArray *sourcesBest = options->sources = psArrayAllocEmpty(matches->n);
-        // XXX something of a hack: require at least 2 detections or the nominated fraction of the max possible
-        //int minMatches = PS_MAX(2, fracMatch * num);// Minimum number of matches required
-        //MEH - if many poor images, will hit oddity with num fraction. use numGoodImages instead
-      int minMatches = PS_MAX(2, fracMatch * numGoodImages);// Minimum number of matches required
+	if (options->photometry) {
+	    // Save best matches for future photometry
+	    psArray *sourcesBest = psArrayAllocEmpty(matches->n);
+
+	    // XXX something of a hack: require at least 2 detections or the nominated fraction of the max possible
+	    //int minMatches = PS_MAX(2, fracMatch * num);// Minimum number of matches required
+	    //MEH - if many poor images, will hit oddity with num fraction. use numGoodImages instead
+	    int minMatches = PS_MAX(2, fracMatch * numGoodImages);// Minimum number of matches required
       
-      int good0 = 0;
-      for (int i = 0; i < num; i++) {
-	if (inputMask->data.U8[i]) {
-	  continue;
+	    int good0 = 0;
+	    for (int i = 0; i < num; i++) {
+		if (inputMask->data.U8[i]) {
+		    continue;
+		}
+		good0 = i;
+		break;
+	    }
+      
+	    for (int i = 0; i < matches->n; i++) {
+		pmSourceMatch *match = matches->data[i]; // Match of interest
+		if (match->num < minMatches) {
+		    continue;
+		}
+
+		// We need to grab a single instance of this source: just take the first available
+		// MEH - if first available is a rejected image, then problem. so take first non-rejected instance above
+		//	 int image = match->image->data.S32[0]; // Index of image
+		// int index = match->index->data.S32[0]; // Index of source within image
+		int image = match->image->data.S32[good0]; // Index of image
+		int index = match->index->data.S32[good0]; // Index of source within image
+		psArray *sources = sourceLists->data[image]; // Sources for image
+		pmSource *source = sources->data[index]; // Source of interest
+
+		// CZW: Accept the first source for all properties, except for the magnitudes.
+		//      We have average magnitudes from relphot, so we should use those
+		pmSource *photsource = matchedSources->data[i];
+		if ((photsource)&&(isfinite(photsource->psfMag))) {
+		    // CZW: photsource is the average relphot mag, which is equal to <m_i + zpIN_j>
+		    //      (primed are averaged values for this discussion)
+		    //      Therefore, the correct magnitude for psfMag' is:
+		    //         psfMag' = (photsource - zpIN_j + magCorr)
+		    //      apMag - psfMag should be retained, so:
+		    //         apMag - psfMag = apMag' - psfMag'
+		    //         apMag' = apMag + psfMag' - psfMag
+		    source->apMag  = source->apMag + (photsource->psfMag + options->norm->data.F32[image] - zp->data.F32[image] - source->psfMag);
+		    source->psfMag = photsource->psfMag + options->norm->data.F32[image] - zp->data.F32[image];
+	   
+		    psTrace("ppStack", 5, "Best Source %d/%ld @ %f %f %f %f %f %d",i,sourcesBest->n,source->peak->xf,source->peak->yf,
+			    source->psfMag,
+			    photsource->psfMag + options->norm->data.F32[image] - zp->data.F32[image] ,
+			    source->psfMagErr,-1);
+		    psArrayAdd(sourcesBest, sourcesBest->n, source);
+		}
+	    }
+	    psLogMsg("ppStack", PS_LOG_INFO, "Selected %ld sources for photometry analysis", sourcesBest->n);
+
+	    // at this point, options->sources contains the full matched list
+	    // here we only keep the best sources:
+	    psFree (options->sources);
+	    options->sources = sourcesBest;
 	}
-	good0 = i;
-	break;
-      }
-      
-      for (int i = 0; i < matches->n; i++) {
-	pmSourceMatch *match = matches->data[i]; // Match of interest
-	 if (match->num < minMatches) {
-	   continue;
-	 }
-
-	 // We need to grab a single instance of this source: just take the first available
-	 // MEH - if first available is a rejected image, then problem. so take first non-rejected instance above
-	 //	 int image = match->image->data.S32[0]; // Index of image
-	 // int index = match->index->data.S32[0]; // Index of source within image
-	 int image = match->image->data.S32[good0]; // Index of image
-	 int index = match->index->data.S32[good0]; // Index of source within image
-	 psArray *sources = sourceLists->data[image]; // Sources for image
-	 pmSource *source = sources->data[index]; // Source of interest
-
-	 // CZW: Accept the first source for all properties, except for the magnitudes.
-	 //      We have average magnitudes from relphot, so we should use those
-	 pmSource *photsource = matchedSources->data[i];
-	 if ((photsource)&&(isfinite(photsource->psfMag))) {
-	   // CZW: photsource is the average relphot mag, which is equal to <m_i + zpIN_j>
-	   //      (primed are averaged values for this discussion)
-	   //      Therefore, the correct magnitude for psfMag' is:
-	   //         psfMag' = (photsource - zpIN_j + magCorr)
-	   //      apMag - psfMag should be retained, so:
-	   //         apMag - psfMag = apMag' - psfMag'
-	   //         apMag' = apMag + psfMag' - psfMag
-	   source->apMag  = source->apMag + (photsource->psfMag + options->norm->data.F32[image] - zp->data.F32[image] - source->psfMag);
-	   source->psfMag = photsource->psfMag + options->norm->data.F32[image] - zp->data.F32[image];
-	   
-	   psTrace("ppStack", 5, "Best Source %d/%ld @ %f %f %f %f %f %d",i,sourcesBest->n,source->peak->xf,source->peak->yf,
-		   source->psfMag,
-		   photsource->psfMag + options->norm->data.F32[image] - zp->data.F32[image] ,
-		   source->psfMagErr,-1);
-	   psArrayAdd(sourcesBest, sourcesBest->n, source);
-	 }
-        }
-      psLogMsg("ppStack", PS_LOG_INFO, "Selected %ld sources for photometry analysis", sourcesBest->n);
-
-    }
-    psFree(zp);
-    psFree(matches);
-
-    return true;
+	psFree(zp);
+	psFree(matches);
+	psFree(matchedSources);
+
+	return true;
 }
Index: /branches/eam_branches/ipp-pstamp-20260421/ppStack/src/ppStackTarget.c
===================================================================
--- /branches/eam_branches/ipp-pstamp-20260421/ppStack/src/ppStackTarget.c	(revision 43018)
+++ /branches/eam_branches/ipp-pstamp-20260421/ppStack/src/ppStackTarget.c	(revision 43019)
@@ -89,4 +89,5 @@
             }
         }
+	psFree (indices);
     }
     psFree(coords);
