clustering code unified