anchor.diff:      Value -> Value
     end,
     {ok, NewConn} = mongo:connect(Host),
  -  proplists:delete(conn, State) ++ [{conn, NewConn}].
  +  proplists:delete(mongo, State) ++ [{mongo, NewConn}].
   
   %@doc Wrapper around mongo:do/5 which is too error-sensitive
   mongo_do(State, Table, Op) ->
  
positive.code:                                       host, bson:utf8(Host),
                                        resource, bson:utf8(Resource),
                                        connected, true}),
      case doc_close_connection(mongo:next(Cursor), JID, null) of
        {ok, NewDoc} -> mongo:save(connections, NewDoc);
        {failure, Reason} ->
          ?DEBUG("Failed to mark connection as closed: ~p", [Reason]),
          failure
      end,
      mongo:close_cursor(Cursor)
  
negative.code:    connected_at, bson:timenow(),
     connected,    true}.
  
  %@doc Marks connection as closed and appends data extracted from agent's JID
  doc_close_connection({}, _) -> {failure, nodoc};
  doc_close_connection({Doc}, {User, _Host, _Resource}) ->
    % Converts a list of tuples ([{x, Val}]) into a list of lists ([[x, Val]]) and flattens it ([x, Val]) for BSON
    AgentData = erlang:list_to_tuple(list:flatmap(fun erlang:tuple_to_list/1, parse_component_jid(User))),
    NewData = {connected, false,
               disconnected_at, bson:timenow(),
               agent_data, AgentData},
    NewDoc = bson:merge(NewData, Doc),
    {ok, NewDoc}.
  
  %@doc Sends an message as mod_copilot@localhost and performs base64-encoding of the attributes
  route_to_copilot_component(To, Xml) -> route_to_copilot_component(?JID, To, Xml).
  