Fixed bug that could end in an infinite loop