 ;
 ; -----\----
 ;       \------
-; [ . . . .  {:cont ---------------------> [ . . . . . ] 
-;             :branch | }]
-;                     \                        
-;                      V                      
-;                      [ . . . . . { . . }]
+; {:id ... :data [ . . . .  {:cont ---------------------> [ . . . . . ] 
+;                            :fork | }]}
+;                                  \                        
+;                                   V                      
+;                                  {:id ... :data [ . . . . . { . . }]}
 
 (defn branch-at [i n]
   (prn "Branching at " i)
   (swap! branch-count inc)
-  (let [index i
+  (let [index (* (dec (len @(@current-branch :data))) (/ i 100))
         new-branch {:id (generate-uuid) :data (atom [])}
         ; split the current branch
         data @(@current-branch :data)
-        [pre-branch post-branch] [(vec (take index data)) (vec (drop index data))]
+        pre-branch (vec (take index data)) 
+        post-branch (vec (drop index data))
         ; insert the branching point into the old branch
-        branched-branch (into [] (concat pre-branch [{:cont post-branch :branch new-branch}]))]
+        branched-branch (conj pre-branch {:cont (atom post-branch) :fork new-branch})]
       ; update the old branch with the inserted version
       (reset! (@current-branch :data) branched-branch)
       ; let current branch pointer point to new branch
       (add-new-branch new-branch)
       (reset! reverted-to -1)))
+
+(defn append-to-branch [branch s]
+  ; in case the branch got forked we have to jump these interruptions
+  (if-let [cont ((last @branch) :cont)]
+    (append-to-branch cont s)
+  (swap! branch conj n)))
 
 (defn save-state [n]
     (cond (> 100 @reverted-to -1 )

      :else
      (let [b @(@current-branch :data)]
        (when-not (= (last b) n)
          (swap! (@current-branch :data) conj n))
        (let [c (len @(@current-branch :data))]
            (prn (str c " Saved " (pluralize c "State")))))))

; (om/root

  @branch-count)

;; global atom referencing the branch that we currently operate on
(def current-branch (atom {:id (generate-uuid) :data (atom [])}))

;; global atom referencing the total history graph
(def app-history (@current-branch :data))

;; ffd
(def history-view)
