I replaced the while loop with an 'if' statement and it seems to work