refine while loop