simplified grouping code
finished grouping function