better group clustering logic