correct loop exit condition as spotted in code review