Fix issue with empty while loop exiting