Updated grouping function to group intensities