refine iterate function