updated logic inside while loop