fixing cycle detection bug