 
 (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]

     :opacity 0.9
     :color "red")))

(defn handle-state-change!
  [_ app-state old-state new-state]

  (when-not (= (:rendering-data old-state)
               (:rendering-data new-state))

    (when-not (= (:rendering-data new-state) (last @rendering-data-history))
      (swap! rendering-data-history conj (:rendering-data new-state)))

    (re-render! app-state))

  (when-not (= (:stats old-state)
               (:stats new-state))

    (render-stats! app-state)))

(defn undo!
  "Revert app-state to the previous value"
  []

             (aget e "ctrlKey"))
    (open-as-png! (:canvas @app-state))))

(defn init!
  "Initialise event handlers, add atom watches, do the first rendering"
  []
