Cycle detection optimized

Stack scan removed.
Tests are OK.