Refactor of iteration logic