made better while loop thanks to vytautas