refine and sort functions completed