Added cycle detection