consolidating grouping code