fix bugs in the iteration