Move grouping calculation down from activity