     (do (.focus element)
        (.setSelectionRange element length length))))
 
-(defn list-view [app owner]
+(defn index-of [coll v]
+  (let [i (count (take-while #(not= v %) coll))]
+    (when (or (< i (count coll))
+            (= v (last coll)))
+      i)))
+
+(defn go-up [app note]
+  (let [existing-list (:notes @app)
+        index (index-of (:notes @app) @note)
+        note-above (get existing-list (- index 1))
+        final-list (notes-with-editing-row-updated existing-list (:id note-above))]
+    (if (> index 0)
+      (om/update! app :notes final-list)
+    )
+  )
+)
+
+(defn go-down [app note]
+  (let [existing-list (:notes @app)
+        index (index-of (:notes @app) @note)
+        last-row-index (- (.-length (clj->js existing-list)) 1)]
+    (if (= index last-row-index)
+      (let [n (new-note (:tab @note))]
+        (save-note app n)
+      )
+      (let [note-above (get existing-list (+ index 1))
+            final-list (notes-with-editing-row-updated existing-list (:id note-above))]
+        (om/update! app :notes final-list)))
+  )
+)
+
+(defn handle-event [type app val]
+  (case type
+    :destroy (destroy-note app val)
+    :edit    (edit-note app val)
+    :up      (go-up app val)
+    :down    (go-down app val)
+    :save    (save-note app val)
+    :cancel  (destroy-note app val)
+    nil))
+
+(defn notes-app [app owner]
   (reify
-    om/IInitState
-    (init-state [_]
-      {:delete (chan)})
-    om/IDidMount
-    (did-mount [this]
-      (focus-on-input)
-    )
-    om/IDidUpdate
-    (did-update [this prev-props prev-state]
-      (focus-on-input)
-    )
     om/IWillMount
     (will-mount [_]
-      (let [delete (om/get-state owner :delete)]
-        (go (loop []
-              (let [note (<! delete)]
-                (om/transact! app :entered
-                  (fn [xs] (vec (remove #(= note %) xs))))
-                (recur))))))
+      (let [comm (chan)]
+        (om/set-state! owner :comm comm)
+        (go (while true
+              (let [[type value] (<! comm)]
+                (handle-event type app value))))))
     om/IRenderState
-    (render-state [this state]
+    (render-state [_ {:keys [comm]}]
       (dom/div nil
         (dom/h1 nil app-title)
         (apply dom/ul nil
-          (om/build-all note-view (:entered app)
-            {:init-state state}))
-        (dom/ul nil
-          (indent-element
-            (dom/li nil (dom/input #js {:type "text" :ref "newNote"
-              :placeholder "Enter note here..."
-              :onKeyDown #(handle-new-note-keydown % app owner)
-              :value (get-in app [:current :title])
-            })) (get-in app [:current :tab])
-          )
-          ;(dom/button #js {:onClick #(add-note app owner)} "Add note")
-        )))))
-
-(defn guid []
-  (.getNextUniqueId (.getInstance IdGenerator)))
-
-(defn add-note [app owner]
-  (let [new-note (-> (om/get-node owner "newNote")
-                        .-value)]
-    (when new-note
-      (om/transact! app :entered #(conj % new-note)))))
+          (om/build-all note-view (:notes app)
+          {:init-state {:comm comm}
+           :key :id }
+          ))))))
 
 (om/root
-  (fn [app owner]
-    (reify om/IRender
-      (render [_]
-        (dom/div nil
-          (om/build list-view app)
-          ))))
+  notes-app
   app-state
   {:target (. js/document (getElementById "app"))})

  (case code
    "enter"
    (do
      (let [new-field (om/get-node owner "newNote")]
      (when-not (string/blank? (.. new-field -value trim))
        (let [new-note {:id (guid)
                        :title (.-value new-field)
                        :status "pending"
                        :tab (get-in @app [:current :tab])}]
          (om/transact! app :entered
            #(conj % new-note)
            [:create new-note])
          (om/transact! app [:current :title]
            (fn [_] "")
            [:clean-current]))
        ))
        false)
    "tab" (do
      (om/transact! app [:current :tab]
        #(+ % 1)
        [:tab nil])
      false)
    "shift+tab" (do
      (om/transact! app [:current :tab]
        #(- % 1)
        [:tab nil])
      false)
    nil
    )))))

(defn focus-on-input []
  (let [element (.querySelector js/document "input")

   40 "down"
   46 "del"
   32 "space"
   27 "escape"
   186 ";"})
 
(defn event-modifiers
