refine algorithm