refine stuff algorithm