refactor the iterative method