Changed if statement to a while statement