Fix occasional infinite loop