Rearanged while loop to avoid cutting off initial solutions.