refine secondary while loop