flawed while loop, has been corrected now