Finally added breaks to exit while loop.