Added a while loop and removed an if/else under for loop