Changed while to if