Refactoring and fixing cohort grouping