put safeguard in while loop