anchor.diff:                                        host, bson:utf8(Host),
                                         resource, bson:utf8(Resource),
                                         connected, true}),
  -    case doc_close_connection(mongo:next(Cursor), JID) of
  +    case doc_close_connection(mongo:next(Cursor)) of
         {ok, NewDoc} -> mongo:save(connections, NewDoc);
         {failure, Reason} ->
           ?DEBUG("Failed to mark connection from ~p as closed: ~p", [JID, Reason]),
  
positive.code:   GeoData = lookup_ip(IPAddress),
    Lng = proplists:get_value(longitude, GeoData),
    Lat = proplists:get_value(latitude, GeoData),
  
    {user,         bson:utf8(User),
     host,         bson:utf8(Host),
     resource,     bson:utf8(Resource),
     loc,          [Lng, Lat],
     connected_at, bson:timenow(),
     connected,    true}.
  
  %@doc Marks connection as closed and appends data extracted from agent's JID
  doc_close_connection({}, _) -> {failure, nosession};
  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(lists: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}.
  
  
negative.code: %@doc Parses JID (username part) of an agent (ex.: agent_s-10829_1_7_18225d4f-e3f7-4c18-9a18-d6c06992d272_-g)
  parse_component_jid(User) ->
    case string:tokens(User, "_") of
      ["agent"|_] = Props -> lists:zip([component, id, cpus, rev, uuid, ukn], lists:map(fun bson:utf8/1, Props));
      [Component|_] -> [{component, bson:utf8(Component)}]
    end.
  
  