anchor.diff:     connected,    true}.
   
   %@doc Marks connection as closed and appends data from agent's JID
  -doc_close_connection({ok, {}}, JID, IPAddress) ->
  -  % There isn't a connection document so we have to create it first
  -  doc_close_connection(doc_create_connection(JID, IPAddress), JID, IPAddress);
  +doc_close_connection({failure, Reason}, _, _) -> {failure, Reason};
  +doc_close_connection({ok, {}}, _, _) -> {failure, nodoc};
   doc_close_connection({ok, {Doc}}, {User, _Host, _Resource}, _IPAddress) ->
     % 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:update(agent_data, NewData, bson:update(connected, false, Doc))
  -  bson:merge(NewData, Doc);
  -doc_close_connection({failure, Failure}, _, _) ->
  -  % Silently fails
  -  ?DEBUG("MongoDB failed in doc_close_connection/3: ~p", [Failure]),
  -  failure.
  +  {ok, bson:merge(NewData, Doc)}.
   
   %@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).
  
positive.code:   end.
  
  %@doc Returns geosplatial information for given IP
  lookup_ip(IPAddress) ->
    ?DEBUG("Decoding IP address ~p", [IPAddress]),
    [_|Data] = erlang:tuple_to_list(element(2, egeoip:lookup(IPAddress))),
  
negative.code:   handle_presence(disconnect, {User, Server, Resource}, null).
  
  %@doc Handles presence stanzas sent by users
  handle_presence(UserState, {_User, Server, _Resource}, IPAddress) ->
    % Processes the stanza in the gen_server
    gen_server:cast(?MODULE, {UserState, JID, IPAddress}),
    report_event(Server, atom_to_list(UserState)),
  