Index: /trunk/ippTools/src/pztool.c
===================================================================
--- /trunk/ippTools/src/pztool.c	(revision 7971)
+++ /trunk/ippTools/src/pztool.c	(revision 7972)
@@ -136,26 +136,23 @@
     for (long i = 0; i < pending->n; i++) {
         pzPendingImfileRow *pendingImfile = pending->data[i];
-        newImfileRow *newImfile = newImfileRowAlloc(
+
+        if (!newImfileInsert(
+            config->dbh, 
             pendingImfile->exp_id,
             pendingImfile->class,
             pendingImfile->class_id,
             // XXX get this from the CLI
-            pendingImfile->uri
-        );
-
-        if (!newImfileInsertObject(config->dbh, newImfile)) {
+            pendingImfile->uri)
+        ) {
             psError(PS_ERR_UNKNOWN, false, "dbh access failed");
-            psFree(newImfile);
-            return false;
-        }
-
-        psFree(newImfile);
-    }
-
-    if (pzPendingImfileDeleteRowObjects(config->dbh, pending, MAX_ROWS)
-        < 0) {
+            psFree(pending);
+            goto ROLLBACK; 
+        }
+    }
+
+    if (pzPendingImfileDeleteRowObjects(config->dbh, pending, MAX_ROWS) < 0) {
         psError(PS_ERR_UNKNOWN, false, "dbh access failed");
         psFree(pending);
-        return false;
+        goto ROLLBACK; 
     }
     psFree(pending);
@@ -164,5 +161,5 @@
     if (!pzsearchFlushPendingExp(config)) {
         psError(PS_ERR_UNKNOWN, false, "pzsearchFlushPendingExp() failed");
-        return false;
+        goto ROLLBACK; 
     }
 
@@ -170,8 +167,17 @@
     if (!psDBCommit(config->dbh)) {
         psError(PS_ERR_UNKNOWN, false, "database error");
-        return false;
-    }
-
-    return true;
+        // XXX is this the right thing to do after a commit failure?
+        goto ROLLBACK; 
+    }
+
+    return true;
+
+ROLLBACK:
+    // rollback
+    if (!psDBRollback(config->dbh)) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
+    }
+
+    return false;
 }
 
@@ -240,5 +246,19 @@
 
         if (!pendingImfiles) {
-            // exp has no coresponding imfiles
+            // exp has no coresponding imfiles so we need to remove it from
+            // pzPendingExp and add it to newExp
+
+            if (!newExpInsert(
+                    config->dbh,
+                    pendingExp->exp_id,
+                    pendingExp->camera,
+                    pendingExp->telescope,
+                    pendingExp->exp_type,
+                    pendingExp->imfiles)
+            ) {
+                psError(PS_ERR_UNKNOWN, false, "dbh access failed");
+                psFree(pending);
+                return false;
+            }
 
             psArray *nukeMe = psArrayAlloc(1);
@@ -252,6 +272,8 @@
             }
         }
-    }
-
-    return true;
-}
+
+        psFree(pendingImfiles);
+    }
+
+    return true;
+}
