         trigger (make-sweep-vector current-z search-params)
         current-exposure (core getExposure)]
     (let [images (acquire-images z-drive trigger (:exposure search-params))
-          scores (map #(score-image "Sum" %) images)
+          scores (map #(score-image (:score-type search-params) %) (seque images))
           best-index (apply max-key #(nth scores %) (range (count images)))
           best-z (nth trigger best-index)]
+      (core stopSequenceAcquisition)
+      (core stopStageSequence z-drive)
       (core setPosition z-drive best-z)
       (core setExposure current-exposure)
       best-z)))
            
 (defn run-test []
   (reset! search-params
-          {:range 10
-           :tolerance 1.00
-           :score-type "Sum"
-           :exposure 10})
-  (run-autofocus @search-params))+          {:range 5
+           :tolerance 0.25
+           :score-type "Sharpness"
+           :exposure 2})
+  (run-autofocus @search-params)
+  (. gui snapSingleImage))
      (core isSequenceRunning)))

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

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

(defn score-image [score-type img]
  (condp = score-type
    "Sum" (apply + img)
    0))

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

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