This check should be part of the while loop.