add the sorted_refine function