done implementation of grouping
not yet tested.