fix iteration logic