Improved overlapping event grouping function