Move check outside of while loop