                  (recur (assoc-safe new-results >= eff b-rts)))
              (let [rec (expand-sa-node b-sa cache (- rec-next-best b-rts) b-s b-rts (- 0 neg-reward b-rts))]
                (when (> (:cutoff rec) Double/NEGATIVE_INFINITY) 
-                 (queues/pq-replace! (queue node) entry (- 0 b-rts (:cutoff rec))))
+                 (queues/pq-replace! (:queue node) entry (- 0 b-rts (:cutoff rec))))
                (doseq [[ss sr] (:result-map rec)]
-                 (queues/pq-add! (queue node)
+                 (queues/pq-add! (:queue node)
                    (SeqNodeEntry ss (when (next b-ra) (get-sa-node cache ss (second b-ra))) sr (next b-ra))
                    (- sr)))
                (recur new-results)))))))

;; TODO: pass incremental updates upwards. 
;; Difficulty: nodes with multiple ancestors.

(defprotocol HasCutoff (cutoff [x]))
(defprotocol HasQueue  (queue [x]))

(deftype PartialResult [result-map cutoff]
  HasCutoff (cutoff [] cutoff))


;; A PartialResult stores a map from states to rewards, where a state is present
;; iff it has reward > cutoff. 


(deftype SANode [context action result-map-atom queue]
  HasCutoff (cutoff [] (- (second (queues/pq-peek-min queue))))
  HasQueue  (queue [] queue))

;; Represents an action sequence from a state, with sanode representing the first action in remaining-actions.
 ; (or nil, if remaining-actions is empty.)

                        [(SANodeEntry s (when (seq ref) (get-sa-node cache s (first ref))) 0.0 ref) 0.0])))))))


(defn cutoff [#^::SANode node]
  (- (second (queues/pq-peek-min (:queue node)))))

(defn stitch-results [effect-map state reward-to-state]
  (util/map-map 
