Index: /trunk/ppSkycell/src/ppSkycellArguments.c
===================================================================
--- /trunk/ppSkycell/src/ppSkycellArguments.c	(revision 23995)
+++ /trunk/ppSkycell/src/ppSkycellArguments.c	(revision 23996)
@@ -55,6 +55,6 @@
     }
 
-    data->imagesName = psMetadataLookupStr(NULL, arguments, "-images");
-    data->masksName = psMetadataLookupStr(NULL, arguments, "-masks");
+    data->imagesName = psMemIncrRefCounter(psMetadataLookupStr(NULL, arguments, "-images"));
+    data->masksName = psMemIncrRefCounter(psMetadataLookupStr(NULL, arguments, "-masks"));
     data->outRoot = psStringCopy(argv[1]);
 
Index: /trunk/ppSkycell/src/ppSkycellLoop.c
===================================================================
--- /trunk/ppSkycell/src/ppSkycellLoop.c	(revision 23995)
+++ /trunk/ppSkycell/src/ppSkycellLoop.c	(revision 23996)
@@ -10,11 +10,4 @@
 
 #define BUFFER 16                       // Size of buffer for projections
-
-// List of input files
-static const char *inFiles[] = { "PPSKYCELL.IMAGE", "PPSKYCELL.MASK", NULL };
-
-// List of output files
-//static const char *outFiles[] = { "PPSKYCELL.JPEG1", "PPSKYCELL.JPEG2", NULL };
-
 
 static void regionMinMax(psRegion *base,// Base region; modified
@@ -41,4 +34,6 @@
     fromCoords->y = (Y); \
     psPlaneTransformApply(toCoords, wcs->trans, fromCoords); \
+    toCoords->x /= wcs->cdelt1; \
+    toCoords->y /= wcs->cdelt2; \
     toCoords->x += wcs->crpix1; \
     toCoords->y += wcs->crpix2; \
@@ -137,5 +132,5 @@
 
     for (int i = 0; i < data->numInputs; i++) {
-        pmFPAfile *file = pmFPAfileSelectSingle(data->config->files, "PPSKYCELL.INPUT", i); // File to examine
+        pmFPAfile *file = pmFPAfileSelectSingle(data->config->files, "PPSKYCELL.IMAGE", i); // File to examine
         // Header in the FPA should have been read as a part of defining the file...
 #if 0
@@ -157,15 +152,15 @@
 
         psRegion *region = imageRegions->data[i] = skycellRegion(wcs, numCols, numRows); // Region of image
+        psTrace("ppSkycell", 5, "Image region %d is: [%.0f:%.0f,%.0f:%.0f]\n",
+                i, region->x0, region->x1, region->y0, region->y1);
 
         bool found = false;             // Found a projection?
         for (int j = 0; j < numProj && !found; j++) {
-            if (wcs->crval1 == crval1->data.F64[j] &&
-                wcs->crval2 == crval2->data.F64[j] &&
-                wcs->cdelt1 == cdelt1->data.F64[j] &&
-                wcs->cdelt2 == cdelt1->data.F64[j]) {
+            if (wcs->crval1 == crval1->data.F64[j] && wcs->crval2 == crval2->data.F64[j] &&
+                wcs->cdelt1 == cdelt1->data.F64[j] && wcs->cdelt2 == cdelt1->data.F64[j]) {
                 regionMinMax(projRegions->data[j], region);
                 target->data.S32[i] = j;
-                skycellRegion(wcs, numCols, numRows);
                 found = true;
+                psTrace("ppSkycell", 3, "Image %d uses projection %d\n", i, j);
             }
         }
@@ -176,6 +171,9 @@
             psVectorAppend(cdelt1, wcs->cdelt1);
             psVectorAppend(cdelt2, wcs->cdelt2);
-            psArrayAdd(projRegions, projRegions->n, region);
+            psRegion *projRegion = psRegionAlloc(region->x0, region->x1, region->y0, region->y1);
+            psArrayAdd(projRegions, projRegions->n, projRegion);
+            psFree(projRegion);
             target->data.S32[i] = numProj;
+            psTrace("ppSkycell", 3, "Image %d uses new projection\n", i);
 
             numProj++;
@@ -187,10 +185,10 @@
     for (int i = 0; i < numProj; i++) {
         psRegion *projRegion = projRegions->data[i]; // Region for skycell projection
-        int xSize = projRegion->x1 - projRegion->x0; // Size of unbinned image
-        int ySize = projRegion->y1 - projRegion->y0; // Size of unbinned image
+        int xSize = projRegion->x1 - projRegion->x0 + 1; // Size of unbinned image
+        int ySize = projRegion->y1 - projRegion->y0 + 1; // Size of unbinned image
         // Size of binned image 1
-        int numCols1 = xSize / (float)data->bin1 + 0.5, numRows1 = ySize / (float)data->bin1 + 0.5;
+        int numCols1 = xSize / (float)data->bin1 + 1.5, numRows1 = ySize / (float)data->bin1 + 1.5;
         // Size of binned image 2
-        int numCols2 = numCols1 / (float)data->bin2 + 0.5, numRows2 = numRows2 / (float)data->bin1 + 0.5;
+        int numCols2 = numCols1 / (float)data->bin2 + 1.5, numRows2 = numRows1 / (float)data->bin2 + 1.5;
 
         psImage *image1 = psImageAlloc(numCols1, numRows1, PS_TYPE_F32); // Binned image
@@ -204,5 +202,9 @@
             }
 
-            fileActivationSingle(data->config, inFiles, true, j);
+            pmFPAfileActivateSingle(data->config->files, true, "PPSKYCELL.IMAGE", j);
+            if (data->masksName) {
+                pmFPAfileActivateSingle(data->config->files, true, "PPSKYCELL.MASK", j);
+            }
+
             pmFPAview *view = filesIterateDown(data->config); // View to readout
             if (!view) {
@@ -230,7 +232,7 @@
             psRegion *imageRegion = imageRegions->data[j]; // Region for image
             // Offsets for image on skycell
-            int xOffset1 = (imageRegion->x0 - projRegion->x0) / (float)data->bin1 + 0.5;
-            int yOffset1 = (imageRegion->y0 - projRegion->y0) / (float)data->bin1 + 0.5;
-            int xOffset2 = xOffset1 / (float)data->bin2 + 0.5, yOffset2 = yOffset1 / (float)data->bin2 + 0.5;
+            int xOffset1 = (imageRegion->x0 - projRegion->x0) / (float)data->bin1;
+            int yOffset1 = (imageRegion->y0 - projRegion->y0) / (float)data->bin1;
+            int xOffset2 = xOffset1 / (float)data->bin2, yOffset2 = yOffset1 / (float)data->bin2;
 
             // XXX Completely neglecting rotations
@@ -242,4 +244,9 @@
             psFree(bin2RO);
             filesIterateUp(data->config);
+            psFree(file->fpa);
+            file->fpa = NULL;
+            pmFPAfileActivate(data->config->files, false, NULL);
+
+            psTrace("ppSkycell", 10, "Blah blah blah\n");
         }
 
