Index: trunk/ppBackground/src/ppBackgroundLoop.c
===================================================================
--- trunk/ppBackground/src/ppBackgroundLoop.c	(revision 28276)
+++ trunk/ppBackground/src/ppBackgroundLoop.c	(revision 28280)
@@ -13,6 +13,7 @@
 {
     pmConfig *config = data->config;                                        // Configuration data
-    pmFPAfile *file = pmFPAfileSelectSingle(config->files, "PPBACKGROUND.BACKGROUND", 0);
+    pmFPAfile *file = pmFPAfileSelectSingle(config->files, "PPBACKGROUND.IMAGE", 0);
     pmFPAfile *patternFile = pmFPAfileSelectSingle(config->files, "PPBACKGROUND.PATTERN", 0); // File with data
+    pmFPAfile *bgFile = pmFPAfileSelectSingle(config->files, "PPBACKGROUND.BACKGROUND", 0);   // File with bg
     pmFPA *patternMosaic = pmFPAConstruct(file->camera, file->cameraName); // Mosaicked FPA for pattern
 
@@ -34,5 +35,5 @@
         }
 
-        if (chip->cell->n > 1) {
+        if (chip->cells->n > 1) {
             psError(PPBACKGROUND_ERR_CONFIG, true,
                     "Input image and background model should be chip-mosaicked");
@@ -44,7 +45,7 @@
             pmFPAfileActivate(config->files, true, "PPBACKGROUND.PATTERN");
 
-            pmCell *cell;               // Cell with pattern data
+            pmCell *patternCell;               // Cell with pattern data
             while ((patternCell = pmFPAviewNextCell(view, patternFile->fpa, 1))) {
-                if (!cell->process || !cell->file_exists) {
+                if (!patternCell->process || !patternCell->file_exists) {
                     continue;
                 }
@@ -68,10 +69,10 @@
                     // Get size of image from concepts
                     if (numCols == 0 || numRows == 0) {
-                        numCols = psMetadataLookupS32(NULL, cell->concepts, "CELL.XSIZE");
-                        numRows = psMetadataLookupS32(NULL, cell->concepts, "CELL.YSIZE");
+                        numCols = psMetadataLookupS32(NULL, patternCell->concepts, "CELL.XSIZE");
+                        numRows = psMetadataLookupS32(NULL, patternCell->concepts, "CELL.YSIZE");
                     }
                     // Get size of image from TRIMSEC
                     if (numCols == 0 || numRows == 0) {
-                        psRegion *trimsec = psMetadataLookupPtr(NULL, cell->concepts, "CELL.TRIMSEC");
+                        psRegion *trimsec = psMetadataLookupPtr(NULL, patternCell->concepts, "CELL.TRIMSEC");
                         numCols = trimsec->x1 - trimsec->x0;
                         numRows = trimsec->y1 - trimsec->y0;
@@ -91,5 +92,5 @@
 
                     // Remove bias sections to avoid warnings
-                    psMetadataItem *biassec = psMetadataLookup(patternRO->parent->concepts, "CELL.BIASSEC");
+                    psMetadataItem *biassec = psMetadataLookup(patternCell->concepts, "CELL.BIASSEC");
                     if (psListLength(biassec->data.V)) {
                         psFree(biassec->data.V);
@@ -123,26 +124,21 @@
         }
 
+        pmChip *patternChip = patternFile ? pmFPAviewThisChip(view, patternMosaic) : NULL; // Chip with pattern
+        pmChip *bgChip = bgFile ? pmFPAviewThisChip(view, bgFile->fpa) : NULL; // Chip with background model
+        if (!ppBackgroundRestore(chip, bgChip, patternChip, view, config)) {
+            psError(psErrorCodeLast(), false, "Unable to replace background");
+            return false;
+        }
+        pmChipFreeData(patternChip);
 
-
-
-
-
-
-                pmHDU *hdu = pmHDUGetLowest(file->fpa, chip, cell); // HDU for readout
-                if (!hdu) {
-                    psError(PS_ERR_UNEXPECTED_NULL, true, "Unable to find HDU for data.");
-                    return false;
-                }
-                if (!hdu->header) {
-                    hdu->header = psMetadataAlloc();
-                }
-                ppBackgroundVersionHeader(hdu->header);
-
-
-
-
-
-
-
+        pmHDU *hdu = pmHDUGetLowest(file->fpa, chip, NULL); // HDU for chip
+        if (!hdu) {
+            psError(PS_ERR_UNEXPECTED_NULL, true, "Unable to find HDU for data.");
+            return false;
+        }
+        if (!hdu->header) {
+            hdu->header = psMetadataAlloc();
+        }
+        ppBackgroundVersionHeader(hdu->header);
 
         // Chip
@@ -158,4 +154,7 @@
     }
 
+    psFree(view);
+    psFree(patternMosaic);
+
     return true;
 }
