Changed code from do while to a while loop