Memberpress: how to add users to groups upon Sign up

In the parallel post on this topic (Bring over permission level from WordPress MemberPress) I came to the following code (this one corrected for curly quotes).

The below code adds people to their corresponding Discourse group when signing up through Memberpress.

BUT.

when they up- or downgrade, it adds them to the new group, but it does NOT remove them from their previous group.

@simon
Simon, any idea how to adjust for that?

Thanks!

add_filter( 'wpdc_sso_params', 'wpdc_custom_sso_params' );
function wpdc_custom_sso_params( $params ) {
	if ( current_user_can( 'mepr-active','memberships:47281' )  ) {
		$params['add_groups'] = 'ForeverFree';
	} else {
		$params['remove_groups'] = 'ForeverFree';
	}

	if ( current_user_can( 'mepr-active','memberships:47295,47297' )  ) {
		$params['add_groups'] = 'CreativeLicense';
	} else {
		$params['remove_groups'] = 'CreativeLicense';
	}

	if ( current_user_can( 'mepr-active','memberships:47299,47301' )  ) {
		$params['add_groups'] = 'JoinLive';
	} else {
		$params['remove_groups'] = 'JoinLive';
	}

	if ( current_user_can( 'mepr-active','memberships:47303,47305' )  ) {
		$params['add_groups'] = 'Transform';
	} else {
		$params['remove_groups'] = 'Transform';
	}

	if ( current_user_can( 'mepr-active','memberships:48259,48238' )  ) {
		$params['add_groups'] = 'TransformPlus';
	} else {
		$params['remove_groups'] = 'TransformPlus';
	}

	return $params;
}