Changed if to while

Nothing major, little improvement