Rearanged while loop to avoid cutting off end solutions.