improved cycle detection