added while loop instead of dowhile