groupBy instead of custom code