         _ (dotimes [i 1000]
             (.assoc! tree i (* 2 i)))
         iterator (iterator tree)]
-    (take 2000 (take-while identity (repeatedly #(.next iterator)))))
+    (= (for [i (range 1000)] [i (* 2 i)]) (map #(.apply vector nil %) (take 2000 (take-while identity (repeatedly #(.next iterator)))))))
+
+  (let [tree (tree 3)
+        iterator (iterator tree)]
+    (.seek iterator -100))
 
   (let [tree (tree 3)
         _ (dotimes [i 1000]


  (let [tree (tree 3)
        iterator (iterator tree)]
    (take 2000 (take-while identity (repeatedly #(.next iterator)))))

  (let [tree (tree 3)

              (assert (every? #(< (aget keys %) (.-lower (aget children (inc %)))) (range (count keys))))
              (dotimes [i (count children)] (.valid! (aget children i)))))))

(deftype Iterator [max-keys ^:mutable node ^:mutable ix ^:mutable end?]
  Object
  (next [this]
        (when-not end?
          (if (nil? (.-children node))
            (do
              (set! ix (+ ix 1))
              (loop []
                (if (>= ix (alength (.-keys node)))
                  (if (instance? Node (.-parent node))
                    (do
                      (set! ix (.-parent-ix node))
                      (set! node (.-parent node))
                      (recur))
                    (do
                      (set! end? true)
                      nil))
                  #js [(aget (.-keys node) ix) (aget (.-vals node) ix)])))
            (do
              (set! node (aget (.-children node) (+ ix 1)))
              (set! ix 0)
              #js [(aget (.-keys node) ix) (aget (.-vals node) ix)]))))
  (seek [this key]
        (when-not end?
          (loop []
            (if (> key (.-upper node))
              (if (instance? Node (.-parent node))
                (do
                  (set! ix (.-parent-ix node))
                  (set! node (.-parent node))
                  (recur))
                (do
                  (set! end? true)
                  nil))
              (do
                (set! ix (.seek node key ix))
                (if (and (not (nil? (.-children node))) (< key (aget (.-keys node) ix)))
                  (do
                    (set! node (aget (.-children node) ix))
                    (set! ix 0)
                    (recur))
                  #js [(aget (.-keys node) ix) (aget (.-vals node) ix)])))))))

(defn iterator [tree]
  (loop [node (.-root tree)]
