Refactor of grouping