Affinity Support

Changes and refactoring for affinity group support