Improve group by impl part 1 of 2