replaced while loop with an until loop