changed to while loop, by adam autens suggestion

(not tested, still probably dosn't work)