added conditional group handler for flexible grouping with conditions.