Index: trunk/ppStack/src/ppStackLoop.c
===================================================================
--- trunk/ppStack/src/ppStackLoop.c	(revision 18346)
+++ trunk/ppStack/src/ppStackLoop.c	(revision 18382)
@@ -213,5 +213,6 @@
     // Preparation iteration: Load the sources, and get a target PSF model
     psTrace("ppStack", 1, "Determining target PSF....\n");
-    psArray *sources = NULL;            // Sources to use for PSF matching
+    psArray *globalSources = NULL;      // Global list of sources for matching (haveSources = TRUE)
+    psArray *indSources = psArrayAlloc(num); // Individual lists of sources for matching (haveSources = FALSE)
     pmPSF *targetPSF = NULL;            // Target PSF
     bool haveSources = psMetadataLookupBool(NULL, config->arguments, "HAVE.SOURCES"); // Global sources?
@@ -240,5 +241,6 @@
                 psError(PS_ERR_UNKNOWN, false, "Unable to find PSF.");
                 psFree(view);
-                psFree(sources);
+                psFree(globalSources);
+                psFree(indSources);
                 psFree(sourceLists);
                 psFree(fileIter);
@@ -257,5 +259,6 @@
                 psError(PS_ERR_UNKNOWN, false, "Unable to determine size of image from PSF.");
                 psFree(view);
-                psFree(sources);
+                psFree(globalSources);
+                psFree(indSources);
                 psFree(sourceLists);
                 psFree(fileIter);
@@ -270,9 +273,16 @@
             if (!haveSources) {
                 pmReadout *ro = pmFPAviewThisReadout(view, inputFile->fpa); // Readout with sources
-                sourceLists = ppStackSourceListAdd(sourceLists, ro, config);
+                psArray *sources = psMetadataLookupPtr(NULL, ro->analysis, "PSPHOT.SOURCES"); // Sources
+                if (!sources) {
+                    psError(PS_ERR_UNEXPECTED_NULL, false, "Unable to find sources in readout.");
+                    return NULL;
+                }
+                sources->data[index] = psMemIncrRefCounter(sources);
+                sourceLists = ppStackSourceListAdd(sourceLists, sources, config);
                 if (!sourceLists) {
                     psError(PS_ERR_UNKNOWN, false, "Unable to add sources to list.");
                     psFree(view);
-                    psFree(sources);
+                    psFree(globalSources);
+                    psFree(indSources);
                     psFree(fileIter);
                     psFree(psfs);
@@ -288,5 +298,6 @@
         if (!targetPSF) {
             psError(PS_ERR_UNKNOWN, false, "Unable to determine output PSF.");
-            psFree(sources);
+            psFree(globalSources);
+            psFree(indSources);
             return false;
         }
@@ -294,6 +305,7 @@
         if (haveSources) {
             // We want to hang on to the 'sources' even when its host FPA is blown away
-            sources = psMemIncrRefCounter(pmFPAfileThisReadout(config->files, view, "PPSTACK.INPUT.SOURCES"));
-            if (!sources) {
+            globalSources = psMemIncrRefCounter(pmFPAfileThisReadout(config->files, view,
+                                                                     "PPSTACK.INPUT.SOURCES"));
+            if (!globalSources) {
                 psError(PS_ERR_UNKNOWN, true, "Unable to find sources.");
                 psFree(view);
@@ -301,7 +313,7 @@
             }
         } else {
-            sources = ppStackSourceListCombine(sourceLists, config);
+            globalSources = ppStackSourceListCombine(sourceLists, config);
             psFree(sourceLists);
-            if (!sources) {
+            if (!globalSources) {
                 psError(PS_ERR_UNKNOWN, false, "Unable to add sources to list.");
                 psFree(view);
@@ -343,5 +355,6 @@
         pmFPAview *view = filesIterateDown(config);
         if (!view) {
-            psFree(sources);
+            psFree(globalSources);
+            psFree(indSources);
             psFree(targetPSF);
             psFree(rng);
@@ -354,4 +367,5 @@
         // Background subtraction, scaling and normalisation is performed automatically by the image matching
         psArray *regions = NULL, *kernels = NULL; // Regions and kernels used in subtraction
+        psArray *sources = haveSources ? globalSources : indSources->data[i]; // Sources for matching
         if (!ppStackMatch(readout, &regions, &kernels, sources, targetPSF, rng, config)) {
             psErrorStackPrint(stderr, "Unable to match image %d --- ignoring.", i);
@@ -374,5 +388,6 @@
         numGood++;
     }
-    psFree(sources);
+    psFree(globalSources);
+    psFree(indSources);
     psFree(targetPSF);
     psFree(rng);
