            (set! node (aget (.-children node) 0)))
          (set! end? (<= (alength (.-keys node)) 0)))
   (key [this]
-       (when (false? end?)
-         (aget (.-keys node) ix)))
+       (aget (.-keys node) ix))
   (val [this]
-       (when (false? end?)
-         (aget (.-vals node) ix)))
+       (aget (.-vals node) ix))
   (next [this]
-        (if (true? end?)
-          false
+        (when (false? end?)
           (if (nil? (.-children node))
-            (do
+            (let [old-node node
+                  old-ix ix]
               (set! ix (+ ix 1))
               (loop []
                 (if (>= ix (alength (.-keys node)))

;; ITERATORS
;; .key / .val return key and val currently pointed at. key may be aliased
;; On creation/reset, points to first key
;; On next, either point to next key or set .-end? true. Return (not .-end?)
;; On seek, point to first key greater than or equal to seek-key. Return (key= (.key this) seek-key)
;; NOTE iterators are not write-safe unless reset after writing

;; TODO think we might need to be able to get last key on end

                      (set! node (.-parent node))
                      (recur))
                    (do
                      (set! node old-node)
                      (set! ix old-ix)
                      (set! end? true)))
                  nil)))
            (do
              (set! node (aget (.-children node) (+ ix 1)))
              (set! ix 0)
              (loop []
                (if (nil? (.-children node))
                  nil
                  (do
                    (set! node (aget (.-children node) 0))
                    (recur))))))))
  (seek [this key]
        (let [start-node node
              start-ix ix]
          (loop []
            (if (and (instance? Node (.-parent node))
                     (or (key-lt (.-upper node) key)
                         (key-lt key (.-lower node))))
              (do
                (set! ix 0)
                (set! node (.-parent node))
                (recur))
              (loop []
                (.-keys node)
                key
                (set! ix (.seek node key 0))
                (if (>= ix (alength (.-keys node)))
                  (if (nil? (.-children node))
                    (do
                      (set! ix (- ix 1)) ;; point to last key
                      (set! end? true))
                    (do
                      (set! node (aget (.-children node) ix))
                      (set! ix 0)
                      (recur)))
                  (if (or (nil? (.-children node))
                          (key= key (aget (.-keys node) ix))
                          (key-lt (.-upper (aget (.-children node) ix)) key))
                    (set! end? false)
                    (do
                      (set! node (aget (.-children node) ix))
                      (set! ix 0)
                      (recur))))))))))

(defn iterator [tree]
  (let [iterator (Iterator. tree (.-root tree) 0 false (.-key-len tree))]
    (.reset iterator)
    iterator))

;; used only inside Join
(deftype JoinIterator [iterator inner->outer inner-key outer-key key-len ^:mutable blocked?]
  Object
  (maintain [this]
            (if (.-end? iterator)
