Cycle detection bugs fixed. Tests are passing.