Fixed an infinite loop bug - I'm not sure what I did is sound.