              (.splice children (+ ix 1) 0 right-child))
            (when (> (alength keys) max-keys)
              (.split! this max-keys)))
-  (split! [this max-keys]
+    (split! [this max-keys]
           ;; TODO try using push/pop instead of splice/slice
-          (let [median (js/Math.floor (/ max-keys 2))
+          (let [median (js/Math.floor (/ (alength keys) 2))
                 median-key (aget keys median)
                 median-val (aget vals median)
                 right-node (Node. parent (+ parent-ix 1) nil nil nil nil upper true aggregateFunc nil)]

                      (when (>= len 0)
                        (set! (.-aggregate this) (aggregateFunc (.-aggregate this) (.getAggregate (aget children len))))
                        (recur (dec len)))))
                  (set! (.-dirty? this) false))
                (.-aggregate this))
  (seek [this key ix]
        (loop [lo (max ix 0)

              (assert (= lower (aget keys 0)) (pr-str lower keys))
              (assert (= upper (aget keys (- (alength keys) 1)))))
            (do
              (assert (= lower (.-lower (aget children 0))) (pr-str lower (.-lower (aget children 0))))
              (assert (= upper (.-upper (aget children (- (alength children) 1)))) (pr-str upper (.-upper (aget children (- (alength children) 1)))))
              (assert (every? #(> (aget keys %) (.-upper (aget children %))) (range (count keys))))
              (assert (every? #(< (aget keys %) (.-lower (aget children (inc %)))) (range (count keys))))
              (dotimes [i (count children)] (.valid! (aget children i) min-keys max-keys))))))

(deftype Iterator [max-keys ^:mutable node ^:mutable ix ^:mutable end?]
  Object
