fixing a reference-counting error resulting from the change to the while loop