Replace the explicit fold and grouping with groupBy.