   new)
 
 (defn- add-w [this key f]
-  (set! (.-watchesArr this) nil)
   (let [w (.-watches this)]
-    (set! (.-watches this) (check-watches w (assoc w key f)))))
+    (set! (.-watches this) (check-watches w (assoc w key f)))
+    (set! (.-watchesArr this) nil)))
 
 (defn- remove-w [this key]
-  (set! (.-watchesArr this) nil)
-  (let [w (.-watches this)
-        r -running]
-    (set! (.-watches this) (check-watches w (dissoc w key)))))
+  (let [w (.-watches this)]
+    (set! (.-watches this) (check-watches w (dissoc w key)))
+    (set! (.-watchesArr this) nil)))
 
 (defn- notify-w [this old new]
   (let [w (.-watchesArr this)

                 (recur (inc i))
                 false))))))

(defn- in-context [obj f]
  (binding [*ratom-context* obj]
    (f)))

(defn- deref-capture [f r]
  (let [watch (.-watching r)]
    (set! (.-captured r) nil)
    (when (dev?)
      (set! (.-ratomGeneration r)
            (set! generation (inc generation))))
    (let [res (in-context r f)
          capt (.-captured r)]
      (set! (.-dirty? r) false)
      (when-not (arr-eq capt watch)
        (._update-watching r capt))
      res)))

(defn- notify-deref-watcher! [derefable]
  (when-some [r *ratom-context*]
    (let [c (.-captured r)]
      (if (nil? c)
        (set! (.-captured r) (array derefable))
        (when (== -1 (.indexOf c derefable))
          (.push c derefable))))))

(defn- check-watches [old new]

          wg watching]
      (set! watching der)
      (doseq [w der]
        (when-not (in-arr wg w) 
          (-add-watch w this handle-reaction-change)))
      (doseq [w wg]
        (when-not (in-arr der w)
          (-remove-watch w this))))
    nil)

