correct loop logic