Fixed an infinite loop bug introduced, but not caught by the tests