 
 (defonce rendering-data-history (atom [initial-rendering-data]))
 
-(defn format-comma
-  [x]
-  (letfn [(format-comma' [x]
-            (->> x
-                 str
-                 reverse
-                 (partition-all 3)
-                 (map reverse)
-                 reverse
-                 (map string/join)
-                 (string/join ",")))]
-    (if (neg? x)
-      (str "-" (format-comma' (* -1 x)))
-      (format-comma' x))))
+(defn handle-state-change!
+  [_ app-state old-state new-state]
 
-(defn render-stats!
-  [app-state]
-  (let [{:keys [render-speed scale current-x current-y current-iterations max-iterations]
-         :as stats}
-        (get @app-state :stats)]
-    (aset (.getElementById js/document "renderSpeed") "innerHTML"
-          (str "Render Speed: " (format-comma render-speed) " px/s"))
+  (when-not (= (:rendering-data old-state)
+               (:rendering-data new-state))
 
-    (aset (.getElementById js/document "scale") "innerHTML"
-          (str "Scale: " (format-comma scale) ))
+    (when-not (= (:rendering-data new-state) (last @rendering-data-history))
+      (swap! rendering-data-history conj (:rendering-data new-state)))
 
-    (aset (.getElementById js/document "maxIterations") "innerHTML"
-          (str "Max Iterations: " (format-comma max-iterations) ))
+    (re-render! app-state))
 
-    (aset (.getElementById js/document "currentX") "innerHTML"
-          (str "Real: " current-x))
+  (when-not (= (:stats old-state)
+               (:stats new-state))
 
-    (aset (.getElementById js/document "currentY") "innerHTML"
-          (str "Imaginary: " current-y ))
-
-    (aset (.getElementById js/document "currentIterations") "innerHTML"
-          (str "Iterations to Escape: " (if (= "infinity" current-iterations)
-                                          "infinity"
-                                          (format-comma current-iterations)) ))))
+    (render-stats! app-state)))
 
 (defn zoom-to!
   [startx starty width height]


  (set! (.-onclick (.getElementById js/document "png")) #(open-as-png! (:canvas @app-state)))

  (set! (.-onclick (.getElementById js/document "toggleStats")) toggle-stats!)


  (re-render! app-state))

                                   :width (/ width scale)
                                   :height (/ height scale)}))))))

(defn handle-mouseup
  "On mouseup, we zoom the mandelbrot to the specified box and remove the
   :mousedown-event key from the app-state"
  [e]

  (let [modulus (fn [x] (Math/sqrt (* x x)))
        startx  (aget (:mousedown-event @app-state) "pageX")
        starty  (aget (:mousedown-event @app-state) "pageY")
        finishx (aget e "pageX")
        finishy (aget e "pageY")]
