Fix while loop duplication