fixed bug in for loop. Replaced with while loop