     (core setPosition z-drive (first trigger))
     (core loadStageSequence z-drive (double-vector trigger))
     (core startStageSequence z-drive)
-    (core waitForDevice z-drive)
     (core startSequenceAcquisition (count trigger) 0 false)
-    (grab-images))
+    (grab-images (count trigger)))
 
 (defn run-autofocus [search-params]
   (let [z-drive (core getFocusDevice)

(ns zippy-focus.core
  (:import [org.micromanager.utils PropertyItem]
           [org.micromanager.api Autofocus])
  (:use [org.micromanager.mm
            :only (load-mm gui mmc core double-vector)])
  (:gen-class

      (core isSequenceRunning)))

(defn pop-burst-image []
  (while (and ;(. mmc isSequenceRunning)
              (zero? (. mmc getRemainingImageCount)))
    (Thread/sleep 1))
  (core popNextImage))

(defn grab-images [n]
    (for [_ (range n) :while (more-images-expected)]
      (pop-burst-image)))

(defn image-stat [img]
  (.. ImageUtils (makeProcessor mmc img) getStatistics))

(defn image-mean [img]
  (.mean (image-stat img)))

(defn image-std-dev [img]
  (let [stat (image-stat img)]
    (/ (.stdDev stat) (.mean stat))))

(defn image-sharpness [img]
  (let [proc (ImageUtils/makeProcessor mmc img)
        mean (.. proc getStatistics mean)
        mean-edge (do (.findEdges proc) (.. proc getStatistics mean))]
    (/ mean-edge mean)))

(defn score-image [score-type img]
  (condp = score-type
    "Mean" (image-mean img)
    "StdDev" (image-std-dev img)
    "Sharpness" (image-sharpness img)
    0))

(defn acquire-images [z-drive trigger exposure]
