anchor.diff:              end
       end.
   
  --spec get_all_directory_keys() -> [?RT:key()].
  +-spec get_all_directory_keys() ->  ?RT:key().
   get_all_directory_keys() ->
  -    [get_directory_by_number(N) || N <- lists:seq(1, ?NUM_DIRECTORIES)].
  -
  --spec get_random_directory() -> ?RT:key().
  -get_random_directory() ->
  +    [get_directory_key_by_number(N) || N <- lists:seq(1, ?NUM_DIRECTORIES)].
  +
  +-spec get_random_directory_key() ->  ?RT:key().
  +get_random_directory_key() ->
       Rand = randoms:rand_uniform(1, ?NUM_DIRECTORIES+1),
  -    get_directory_by_number(Rand).
  -
  --spec get_directory_by_number(pos_integer()) -> ?RT:key().
  -get_directory_by_number(N) when N > 0 ->
  +    get_directory_key_by_number(Rand).
  +
  +-spec get_directory_key_by_number(pos_integer()) -> ?RT:key().
  +get_directory_key_by_number(N) when N > 0 ->
       ?RT:hash_key("lb_active_dir" ++ int_to_str(N)).
   
  -post_load_to_directory(Load, Directory) ->
  +-spec post_load_to_directory(node(), directory_name()) -> ok.
  +post_load_to_directory(Load, DirKey) ->
       TLog = api_tx:new_tlog(),
  -    case api_tx:read(TLog, Directory) of
  +    case api_tx:read(TLog, DirKey) of
           {TLog2, {ok, Content}} ->
  -            ContentNew = gb_sets:add(Load, Content),
  -            {TLog3, _Result} = api_tx:write(TLog2, Directory, ContentNew),
  +            ContentNew = dir_add_load(Load, Content),
  +            {TLog3, _Result} = api_tx:write(TLog2, DirKey, ContentNew),
               case api_tx:commit(TLog3) of
                   {ok} ->
                       ok;
  -                {fail, abort, [Directory]} ->
  +                {fail, abort, [DirKey]} ->
                       log:log(warn, "~p: Failed to write to directory, retrying...", [?MODULE]),
  -                    post_load_to_directory(Load, Directory)
  +                    post_load_to_directory(Load, DirKey)
               end;
           {_TLog2, {fail, not_found}} ->
               log:log(warn, "~p: Directory not found while posting load. This should never happen...", [?MODULE]),
               ok
       end.
   
  -write_to_directory() -> ok.
  -
  -read_from_directory() -> ok.
  +-spec dir_add_load(node(), directory()) -> directory().
  +dir_add_load(Load, Directory) ->
  +    Pool = Directory#directory.pool,
  +    PoolNew = gb_sets:add(Load, Pool),
  +    Directory#directory{pool = PoolNew}.
  +
  +-spec pop_load_in_directory(directory_name()) -> directory().
  +pop_load_in_directory(DirKey) ->
  +    TLog = api_tx:new_tlog(),
  +    case api_tx:read(TLog, DirKey) of
  +        {TLog2, {ok, Directory}} ->
  +            case api_tx:req_list(TLog2, [{write, DirKey, dir_clear_load(Directory)}, {commit}]) of
  +                {[], ok, ok} -> Directory;
  +                _ -> pop_load_in_directory(DirKey)
  +            end;
  +        {_TLog2, {fail, not_found}} ->
  +            log:log(warn, "~p: Directory not found while posting load. This should never happen...", [?MODULE])
  +    end.
  +
  +-spec dir_clear_load(directory()) -> directory().
  +dir_clear_load(Directory) ->
  +    Directory#directory{pool = gb_sets:new()}.
  +
  +dir_set_schedule(Schedule, Directory) ->
  +    Directory#directory{schedule = Schedule}.
  +
  +get_directory(DirKey) ->
  +    TLog = api_tx:new_tlog(),
  +    case api_tx:read(TLog, DirKey) of
  +        {TLog2, {ok, Directory}} -> 
  +            {TLog2, Directory};
  +        _ -> 
  +            log:log(warn, "~p: Directory not found while posting load. This should never happen...", [?MODULE]),
  +            get_directory(DirKey)
  +    end.
  +
  +-spec set_directory(api_tx:tlog(), directory()) -> ok | failed.
  +set_directory(TLog, Directory) ->
  +    DirKey = Directory#directory.name,
  +    case api_tx:req_list(TLog, [{write, DirKey, Directory}, {commit}]) of
  +        {[], ok, ok} -> 
  +            ok;
  +        _  ->
  +            log:log(warn, "~p: Failed to save directory ~p because of failed transaction.", [?MODULE, DirKey]),
  +            failed
  +    end.
   
   %%%%%%%%%%%%
   %% State
  
positive.code: create_directories(0) ->
      ok;
  create_directories(N) when N > 0 ->
      Key = int_to_str(get_directory_by_number(N)),
      TLog = api_tx:new_tlog(),
  
      case api_tx:read(TLog, Key) of
  
negative.code: 
  -type utilization() :: float().
  
  -record(node, {utilization = ?required(node, utilization)    :: utilization(), %% erlang sorts by first element in record/tuple
                 pid      = ?required(node, pid)               :: comm:mypid(),
                 capacity = 1                                  :: number(),
                 load     = ?required(node, load)              :: number()
                 }).
  
  -type directory_name() :: string().
  
  -record(directory, {name         = ?required(directory, name) :: directory_name(), 
                      last_balance = nil                        :: nil | os:timestamp(),
                      pool         = gb_sets:new()              :: gb_set(),
                      schedule     = []                         :: [reassign()]
                      }).
  
  -record(reassign, {from = ?required(reassign, from) :: node:node_type(),
                     to   = ?required(reassign, to)   :: node:node_type()
                     }).
  
  -type reassign() :: #reassign{}.
  
  -record(state, {capacity = 1500, %% free cpu usage, free memory, bandwith...
                  my_dirs = [],
  