make grouping function more stable