Renamed some variables and explicitly freed memory in the group method.