fix iteration end condition