Index: trunk/ippTools/src/pztool.c
===================================================================
--- trunk/ippTools/src/pztool.c	(revision 15159)
+++ trunk/ippTools/src/pztool.c	(revision 15167)
@@ -37,5 +37,5 @@
 
 static bool copydoneCompleteExp(pxConfig *config);
-static psArray *pzGetKnownCameras(pxConfig *config);
+static psArray *pzGetPendingCameras(pxConfig *config);
 static psArray *pzArrayZip(psArray *arraySet);
 
@@ -220,5 +220,5 @@
     }
 
-    psArray *cameras = pzGetKnownCameras(config);
+    psArray *cameras = pzGetPendingCameras(config);
     if (!cameras) {
         psError(PXTOOLS_ERR_DATA, false, "failed to find any cameras");
@@ -229,4 +229,8 @@
     psArray *cameraImfiles = psArrayAlloc(0);
 
+    // the total number of imfiles retreived so far
+    long imfiles = 0;
+    // any slots left over by a query returning less than the per camera limit
+    long leftOvers = 0;
     for (long i = 0; i < psArrayLength(cameras); i++) {
         psString query = pxDataGet("pztool_pendingimfile.sql");
@@ -250,10 +254,13 @@
         if (limit) {
             // in the case where the limit asked for is less then the number of
-            // cameras we have, return 1 imfile per camera
+            // cameras we have, return 1 imfile per camera and we'll stop
+            // making per camera queries when we have reached limit.
             if (limit < psArrayLength(cameras)) {
                 camLimit = 1;
             } else {
                 camLimit = limit / psArrayLength(cameras);
-                // add the modulous to the first camera in the list
+                // add the modulous to the first camera in the list, so if
+                // limit is not even divsable by the number of cameras, the
+                // left overs don't get dropped on the floor
                 if (i == 0) {
                     camLimit += limit % psArrayLength(cameras);
@@ -264,4 +271,7 @@
         // treat limit == 0 as "no limit"
         if (camLimit) {
+            // pickup the left overs from the prevous camera
+            camLimit += leftOvers;
+
             // divide limit by the number of cameras
             psString limitString = psDBGenerateLimitSQL(camLimit);
@@ -290,7 +300,21 @@
         }
 
+        // if we got less rows then the limit for this camera, give the extra
+        // slots to the next camera.
+        if (camLimit) {
+            leftOvers = camLimit - psArrayLength(result);
+        }
+
+        // update the total count of imfiles fetched
+        imfiles += psArrayLength(result);
+
         // add this query into the array of result set
         psArrayAdd(cameraImfiles, 0, result);
         psFree(result);
+
+        // check to see if we've retreived enough imfiles
+        if (imfiles >= limit) {
+            break;
+        }
     }
 
@@ -601,8 +625,8 @@
 }
 
-static psArray *pzGetKnownCameras(pxConfig *config)
+static psArray *pzGetPendingCameras(pxConfig *config)
 {
     // get a list of cameras we've seen exps for
-    if (!p_psDBRunQuery(config->dbh, "SELECT DISTINCT camera FROM summitExp")) {
+    if (!p_psDBRunQuery(config->dbh, "SELECT DISTINCT camera FROM pzPendingImfile")) {
         psError(PS_ERR_UNKNOWN, false, "database error");
         return false;
