Added cycle detection and test