improved the next_step algorithm