     [[] (assoc state :focus xy)]
     
     [[:edit xy] :focus? false]
-    (if (= (:mode state) :formula)
-      [[] state]
+    (case (:mode state)
+
+      :formula [[] state]
+
       [[(display-mode xy)
         [[:display xy] :value (cell-value (:focus state) state)]]
        (assoc state :mode :done)])
 
     [[:edit xy] :value v]
     (if (formula? v)
-      [[(formula-mode xy)]
-       (-> (assoc state :mode :formula)
-           (assoc-in [:cells xy :formula] v))]
-      [[] (-> (assoc state :mode :value)
-              (assoc-in [:cells xy :value] v))])
+      [[(formula-mode xy)] (assoc state :mode :formula xy {:formula v})]
+      [[]                  (assoc state :mode :value   xy {:value   v})])
 
     [[:edit xy] :key-down 13]
     [[(display-mode xy)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Helpers

(defn- formula? [s]
  (string? (first (re-matches #"^=.*" s))))

(defn- write-formula [s state]
  (let [formula (get-in state [:cells (:focus state) :formula])]
    (str formula " " s)))

(defn- cell-value [xy state]
  (let [cell (get-in state [:cells xy])]
    (if (:formula cell)
      (read-formula (:formula cell) state)
      (:value cell))))

  (match message

    [[:display xy] :click _]
    (case (:mode state)

      :formula
      (let [formula (write-formula xy state)]
        [[(source-mode xy)
          (display-mode (:source state))
          [[:edit (:focus state)] :value formula]]
         (assoc state :source xy (:focus state) {:formula formula})])

      [[(edit-mode xy)
        (display-mode (:edit state))]
       (assoc state :edit xy)])
