          (util/cond-let [x]
           ;; Greedy -- all preconditions satisfied and not assigned elsewhere
           (make-greedy-fire-action s effect-var)
-            [[x]]
+            (do ;(print "g") (flush) 
+              [[x]])
+
+          ;; Real deadlock - fail
+          (deadlocked? s child-var-map [])
+            (do ;(print "d") (flush) 
+                nil)
+            
+          ;; Deadlocked by something out-of-context -- exit  (note -- need to check for goal means
+            ; we should catch these issues earlier..
+          (some #(precond-deadlocked-ooc? s % effect-var child-var-map ancestor-vars) (keys reduced-pm))
+            (do ;(print "x") (flush)
+                (when-not (= effect-var sas/goal-var-name) [[]]))
 
           ;; Active precondition -- assigned, needs its value changed, not deadlock               
+          ;; TODO: order
           (util/find-first #(and (env/get-var s (parent-var % effect-var))
                                  (not (= (env/get-var s %) (reduced-pm %)))
                                  (not (precond-deadlocked? s % effect-var)))
                            (keys reduced-pm))
-            [[(make-achieve-precondition-hla hierarchy x (reduced-pm x)) this]]
+            (do ;(print "a" (if (env/get-var s (action-var x)) "o" "n")) (flush)
+             [[(make-achieve-precondition-hla hierarchy x (reduced-pm x)) this]])
 
           ;; Inactive precondition -- needs to be assigned.  
-          (util/find-first #(env/get-var s (free-var %)) (keys reduced-pm))
-            (for [a (activation-actions child-var-map x)]
-              [a this])
+          (util/find-first #(and (env/get-var s (free-var %))
+                                 (not (= (env/get-var s %) (reduced-pm %))))
+                           (keys reduced-pm))
+            (do ;(print "i") (flush) 
+             (for [a (activation-actions child-var-map x)]
+               [a this]))
 
           ;; Active top-down var -- add actions
           (util/find-first #(not (env/get-var s (free-var %))) (map second na-tuples))  
-            (for [as (vals (util/safe-get-in (:dtgs hierarchy) [x (env/get-var s x)])), a as]
-              [(make-add-action-action a) this])
+            (do (println "A!");(print "a") (flush)
+             (for [as (vals (util/safe-get-in (:dtgs hierarchy) [x (env/get-var s x)])), a as]
+               [(make-add-action-action a) this]))
 
           ;; Inactive top-down var -- activate it
           (first (map second na-tuples))
-            (for [a (activation-actions child-var-map x)]
-              [a this])
+            (do (println "I!"); (flush)
+             (for [a (activation-actions child-var-map x)]
+               [a this]))
             
           ;; Nothing to do here -- return upwards, or fail if goal-var
           :else
-            (when-not (= effect-var sas/goal-var-name) [[]]))))
+            (do ;(print "f") (flush)
+              (when (and (= effect-var sas/goal-var-name) (not (deadlocked? s child-var-map  nil)))
+                (def *bs* s))
+              (when-not (= effect-var sas/goal-var-name) [[]])))))
        (cycle-level-           [s] nil))))
 
-
+;(def *bs* nil)
 
  ; include ancestor vars, action vars, free vars, in-pointing 'rents
 (defn make-asplan-skip-henv [sas-problem] 
   (let [env    (make-asplan-env sas-problem)
         cg     (:causal-graph env)
         cvm    (:child-var-map env)
-        av-map (into {} (for [v (keys (:vars sas-problem))] [v (graphs/ancestor-set cg [v])]))
+        av-map (:ancestor-var-map env)
         pc-map (into {} 
                  (for [[k as] av-map] 
                    [k 

          :when (and (= s cur-val) (not (contains? (backward-sets t) s)))]
      t)))


(defn activation-actions  "Return a list of all activation actions for var v"
  [child-map v]
  (for [c (child-map v)]
    (make-set-parent-var-action v c)))

(defn deadlocked? [s child-map extra-edges]
  (let [parent-edges (concat extra-edges 
                       (for [[p cs] child-map, c cs :when (env/get-var s (parent-var p c))] [p c]))
        res-map      (into {} (filter (fn [[p c]] 
                                        (when-let [a (env/get-var s (action-var c))]
                                          (contains? (:precond-map a) p))) 
