Fixed error in while loop by using break