Refactoring group by