fixed iterative code