Repaired the cycle detection algorithm.
Added a debug print.