refactor: sorting close to grouping