iterative implementation