Added bad cycle detection, not fully working