     (swap! state assoc-in [:last-stage-positions (core getFocusDevice)]
            (last absolute-slices))))
 
-(defn pop-burst-image
-  "Returns a TaggedImage from the circular buffer -- times out if no
-   image is found in circular buffer after waiting up to timeout-ms."
+(defn pop-tagged-image []
+  (try (core popNextTaggedImage)
+       (catch Exception e nil))) ;(println e))))
+
+(defn pop-tagged-image-timeout
   [timeout-ms]
   (let [start-time (System/currentTimeMillis)]
-    (while (and (. mmc isSequenceRunning)
-                (zero? (. mmc getRemainingImageCount)))
-      (if (< timeout-ms (- (System/currentTimeMillis) start-time))
-        (throw (Exception. "Timed out waiting for image to arrive from camera."))
-        (Thread/sleep 5))))
-  (let [tagged-image (core popNextTaggedImage)]
+    (loop []
+      (when (keep-collecting-from-circular-buffer?)
+        (if-let [image (pop-tagged-image)]
+          image
+          (if (< timeout-ms (- (System/currentTimeMillis) start-time))
+            (do (println "Timed out waiting for image to arrive from camera.")
+                (throw (Exception. "Timed out waiting for image to arrive from camera.")))
+            (do (Thread/sleep 1)
+                (recur))))))))
+
+(defn pop-burst-image
+  [timeout-ms]
+  (let [tagged-image (pop-tagged-image-timeout timeout-ms)]
+    ;(println "pop")
+        ;(println tagged-image)
     {:pix (.pix tagged-image)
      :tags (json-to-data (.tags tagged-image))}))
 

(defn first-trigger-missing? []
  (= "1" (get-property-value (core getCameraDevice) "OutputTriggerFirstMissing")))

(defn init-burst [length trigger-sequence relative-z]
  (core setAutoShutter (@state :init-auto-shutter))
  (load-property-sequences (:properties trigger-sequence))

                  (assoc :camera-channel-index cam-chan))
        time-stamp (burst-time (:tags image) @state)]
    (annotate-image image event @state time-stamp)))

(defmacro doseq-parallel [seq-exprs & body]
  `(let [pool# (-> (Runtime/getRuntime) .availableProcessors Executors/newFixedThreadPool)]
     (println (.getCorePoolSize pool#))
     (doseq ~seq-exprs
       (let [body-fn# (cast Runnable (fn []
                                       ;(println (str (Thread/currentThread)))
                                       ~@body))]
         (.submit pool# body-fn#)))
     (.shutdown pool#)
     (.awaitTermination pool# 10 TimeUnit/DAYS)))

(defn produce-burst-images
  "Pops images from circular buffer and sends them to output queue."
