   ([block] (mine block (atom true)))
   ([block switch]
    (let [attempt (hashed block)]
+     (when (= 0 (mod (get-in attempt [:header :nonce]) 1000000))
+       (println "got to nonce: " (get-in attempt [:header :nonce])))
      (if (meets-target? attempt)
        attempt
-       (recur (update-in block [:header :nonce] inc)
-              switch)))))
+       (if (not @switch)
+         (do (println "exiting")
+             nil)
+         (recur (update-in block [:header :nonce] inc)
+              switch))))))
 
 (defn coinbase []
   {:inputs []

  []
  (let [recent-blocks (take-last 10 @bc/block-chain)]
    (if (and recent-blocks (> (count recent-blocks) 5))
      (adjusted-target recent-blocks 30)
      (hex-string (math/expt 2 234)))))

(defn generate-block
  [transactions]

  (into []
        (map txn/hash-txn [(coinbase)])))

(defonce mine? (atom true))
(defn stop-miner! [] (reset! mine? false))

(defn mine-block
  ([] (mine-block (generate-block (gather-transactions))))
  ([pending]
   (println "****** Will Mine Block: ******\n" pending "\n***************************")
   (if-let [b (mine pending mine?)]
     (do (println "****** Successfully Mined Block: ******\n" b "\n***************************")
         (bc/add-block! b))
     (println "didn't find coin, exiting"))))

(defn run-miner! []
  (reset! mine? true)
