         _ (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]

              (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]
  ;; always points to before a valid key or to the tree wrapper
  Object
  (next [this]
        (when (instance? Node node)
          (if (nil? (.-children node))
            (do
              (set! ix (+ ix 1))
              (loop []
                (when (and (instance? Node node) (>= ix (alength (.-keys node))))
                  (set! ix (.-parent-ix node))
                  (set! node (.-parent node))
                  (recur))))
            (do
              (set! node (aget (.-children node) (+ ix 1)))
              (set! ix 0)))
          (when (instance? Node node)
            #js [(aget (.-keys node) ix) (aget (.-vals node) ix)])))
  (seek [this key] ;; move the iterator forwards until it reaches a key greater than this one
        ;; head across and upwards until we reach a greater key
        (loop []
          (when (instance? Node node)
            (set! ix (.seek node key ix))
            (when (and (>= ix (alength (.-keys node))) (instance? Node (.-parent node)))
              (set! ix (.-parent-ix node))
              (set! node (.-parent node))
              (recur))))
        ;; head downwards and across until we reach the least greater key
        (loop []
          (when (instance? Node node)
            (when-not (or (nil? (.-children node)) (== key (aget (.-keys node) ix)))
              (set! node (aget (.-children node) ix))
              (set! ix (.seek node key 0))
              (recur))))
        ;; if we aren't now at the tree wrapper we can return a result
        (when (and (instance? Node node) (< ix (alength (.-keys node))))
          #js [(aget (.-keys node) ix) (aget (.-vals node) ix)])))

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


  (let [tree (tree 3)
        iterator (iterator tree)]
    (.next iterator))

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

  (let [tree (tree 3)
