   ;; TODO worth doing binary search here when nodes are large
   (seek [this key ix]
         (let [len (alength keys)]
-          (loop [ix ix]
+          (loop [ix (max ix 0)]
             (if (and (< ix len) (> key (aget keys ix)))
               (recur (+ ix 1))
               ix))))
   (assoc! [this key val max-keys]
           (let [ix (.seek this key 0)]
             (if (nil? children)
-              (if (= key (aget keys ix))
+              (if (== key (aget keys ix))
                 (do
                   (aset vals ix val)
                   true)

  (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 []
          (if (< ix (alength (.-keys node)))
            (if (<= key (aget (.-keys node) ix))
              ;; done
              nil
              ;; move along
              (do
                (set! ix (+ ix 1))
                (recur)))
            (let [parent (.-parent node)]
              (if (instance? Node parent)
                ;; jump into parent and start again
                (do
                  (set! ix (+ (.-parent-ix node) 1))
                  (set! node parent)
                  (recur))
                ;; end of tree
                nil))))
        ;; head downwards and across until we reach the least greater key
        (loop []
          (if (< ix (alength (.-keys node)))
            (if (<= key (aget (.-keys node) ix))
              ;; check for a child
              (if-let [child (and (.-children node) (> ix 0) (aget (.-children node) (- ix 1)))]
                ;; jump into child and start again (child cannot be empty if tree is correctly balanced)
                (do
                  (set! node child)
                  (set! ix 0)
                  (recur))
                ;; done
                nil)
              ;; move along
              (do
                (set! ix (+ ix 1))
                (recur)))
            ;; end of tree
            nil))
        (if (< ix (alength (.-keys node)))
          #js [(aget (.-keys node) ix) (aget (.-vals node) ix)]
          nil)))

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

     (dotimes [i 100000]
       (.assoc! tree i (* 2 i)))))

  (time
   (let [tree (tree 100)]
     (dotimes [i 500000]
       (.assoc! tree i (* 2 i)))))

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