 (declare make-htn-methods)
 
 ;; walk ir, look for pclasses that have non-empty methods
+;; (defn transform-htn [ir]
+;;   (reset! *debug-ir* ir) ;;DEBUG
+;;   (let [ir-syms (seq ir)]
+;;     (loop [[k v] (first ir-syms) more (rest ir-syms)]
+;;       (if-not k
+;;         nil
+;;         (let [{:keys [type methods]} v]
+;;           ;; (println "TRANSFORM-HTN" k)
+;;           (when (and (= type :pclass) methods)
+;;             (let [method-syms (seq methods)]
+;;               (loop [[mname method] (first method-syms) moar (rest method-syms)]
+;;                 (if mname ;; else (println "no more methods")
+;;                   (let [{:keys [temporal-constraints args primitive body]} method]
+;;                     ;; (println "  METHOD" mname "PRIMITIVE" primitive)
+;;                     (if (and (not primitive) body)
+;;                       (make-htn-methods ir k mname args body))
+;;                     (recur (first moar) (rest moar)))))))
+;;           (recur (first more) (rest more)))))))
+
+;; walk ir, look for pclasses that have non-empty methods
 (defn transform-htn [ir]
-  (let [ir-syms (seq ir)]
-    (loop [[k v] (first ir-syms) more (rest ir-syms)]
-      (if-not k
-        nil
-        (let [{:keys [type methods]} v]
-          ;; (println "TRANSFORM-HTN" k)
-          (when (and (= type :pclass) methods)
-            (let [method-syms (seq methods)]
-              (loop [[mname method] (first method-syms) moar (rest method-syms)]
-                (if mname ;; else (println "no more methods")
-                  (let [{:keys [temporal-constraints args primitive body]} method]
-                    ;; (println "  METHOD" mname "PRIMITIVE" primitive)
-                    (if (and (not primitive) body)
-                      (make-htn-methods ir k mname args body))
-                    (recur (first moar) (rest moar)))))))
-          (recur (first more) (rest more)))))))
+  (reset! *debug-ir* ir) ;;DEBUG
+  (doseq [[pclass-name v] (seq ir)]
+    (assert pclass-name "Found a nil pclass in the IR")
+    (let [{:keys [type methods]} v]
+      ;; (println "TRANSFORM-HTN" pclass-name)
+      (when (= type :pclass) ;;Could it be anything else?
+        (doseq [[mname method] (seq methods)]
+          (if mname ;; else (println "no more methods")
+            (let [{:keys [temporal-constraints args primitive display-name body]} method]
+              ;; (println "  METHOD" mname "PRIMITIVE" primitive)
+              (if (and (not primitive) body)
+                (make-htn-methods ir pclass-name mname display-name args body))
+              )))))))
 
 ;; consider memoizing or caching result
 (defn find-plant-fn-bounds [ir plant-fn-pclass ctor-arg-i method]

           ancestry-path
           pclass
           name
           nonprimitive-task ;;This is the HTN-nonprimitive-task that this method matches on
           preconditions
           subtasks ;;This is a simple list of the tasks


(defn pprint-htn-methods []
  (let [htn-methods @*htn-methods*
        pclasses (sort (keys htn-methods))]
    (println "PPRINT-HTN-METHODS")
    (doseq [pclass pclasses]
      (println "PCLASS" pclass "METHODS" (sort (keys (get htn-methods pclass)))))))

;; returns a sequence of [k v] pairs containing
;; methods which match name in pclass
;; or, if empty, matches in all other pclasses
(defn find-candidate-methods [ir pclass name]
  (let [htn-methods @*htn-methods*
        pclasses (keys htn-methods)
        all-pclasses (set (keys ir))
        other-pclasses (set/difference all-pclasses (set pclasses))]
    ;; DEBUG
    ;; (println "FCM PCLASS" pclass "NAME" name "PCLASSES" pclasses "OTHER-PCLASSES" other-pclasses)
    (loop [in-pclass [] ;; methods inside pclass
           out-pclass [] ;; methods outside of pclass
           pc (first pclasses)
           more (rest pclasses)]
      (if-not pc
        (sort-by first ;; return the methods sorted by method name
          (if-not (empty? in-pclass)
