anchor.diff:          end ||
           Group <- Groups].
   
  -leave_update_remote_map(Pid, RemoteMap, Groups) when is_pid(Pid) ->
  -    leave_update_remote_map([Pid], RemoteMap, Groups);
  -leave_update_remote_map(Pids, RemoteMap, Groups) ->
  +leave_remote(Pid, RemoteMap, Groups) when is_pid(Pid) ->
  +    leave_remote([Pid], RemoteMap, Groups);
  +leave_remote(Pids, RemoteMap, Groups) ->
       lists:foldl(
           fun (Group, Acc) ->
               case maps:get(Group, Acc) -- Pids of
  
positive.code:             true = ets:insert(Scope, {Group, AllNewPids, LocalPids}),
              sync_groups(Scope, NewRemoteGroups, Tail);
          error ->
              join_remote(Scope, Group, Pids),
              sync_groups(Scope, RemoteGroups, Tail)
      end.
  
  join_monitors(Pid, Group, Monitors) when is_pid(Pid) ->
      case maps:find(Pid, Monitors) of
          {ok, {MRef, Groups}} ->
              maps:put(Pid, {MRef, [Group | Groups]}, Monitors);
          error ->
              MRef = erlang:monitor(process, Pid),
              Monitors#{Pid => {MRef, [Group]}}
      end;
  join_monitors([], _Group, Monitors) ->
      Monitors;
  join_monitors([Pid | Tail], Group, Monitors) ->
      join_monitors(Tail, Group, join_monitors(Pid, Group, Monitors)).
  
  join_local_group(Scope, Group, Pid) when is_pid(Pid) ->
      case ets:lookup(Scope, Group) of
          [{Group, All, Local}] ->
              ets:insert(Scope, {Group, [Pid | All], [Pid | Local]});
          [] ->
              ets:insert(Scope, {Group, [Pid], [Pid]})
      end;
  join_local_group(Scope, Group, Pids) ->
      case ets:lookup(Scope, Group) of
          [{Group, All, Local}] ->
              ets:insert(Scope, {Group, Pids ++ All, Pids ++ Local});
  
negative.code:     %% ETS table name, and also the registered process name (self())
      scope :: atom(),
      %% monitored local processes and groups they joined
      local = #{} :: #{pid() => {MRef :: reference(), Groups :: [group()]}},
      %% remote node: scope process monitor and map of groups to pids for fast sync routine
      remote = #{} :: #{pid() => {reference(), #{group() => [pid()]}}}
  }).
  
  -type state() :: #state{}.
  