Refined the way the sorting works