Refactor aggregation to make it easier to do group aggregation