better performance for cycle detection algo