Initial refactoring and implementation of Group/Ungroup