fix infinite loop, hopefully maintaining correctness