Adding grouping algorithm.