moved calc_grouping to utils