anchor.diff:  
   
   %%====================================================================
  +
   %% @private
  -init([]) ->
  -    random:seed(now()),
  -    _Tid = ets:new(?TAB, [bag, protected, named_table, {keypos, #chunk.idt}]),
  -    D = dict:new(),
  -    {ok, #state{ torrent_dict = D }}.
  -
  -%% @private
  -handle_call({new, Id}, {Pid, _Tag}, S) ->
  -    ManageDict = dict:store(Pid, Id, S#state.torrent_dict),
  -    _ = erlang:monitor(process, Pid),
  -    {reply, ok, S#state { torrent_dict = ManageDict }};
  +init([TorrentID]) ->
  +    true = register_chunk_server(TorrentID),
  +
  +    %% Load some information about this torrent from higher up
  +    %% in the supervisor-tree.
  +    {value, NumPieces} = etorrent_torrent:num_pieces(TorrentID),
  +    FetchedPieces = etorrent_piece_mgr:fetched(TorrentID),
  +    PiecesValid   = etorrent_pieceset:from_list(FetchedPieces, NumPieces),
  +    PiecesChunked = etorrent_pieceset:new(NumPieces),
  +
  +    %% Initialize a full chunkset for all pieces that are left to download
  +    %% but don't mark them as chunked before a chunk has been deleted from
  +    %% any of the chunksets.
  +    AllIndexes = lists:seq(0, NumPieces - 1),
  +    AllPieces = etorrent_pieceset:from_list(AllIndexes, NumPieces),
  +    PiecesInvalid = etorrent_pieceset:difference(AllPieces, PiecesValid),
  +    ChunkList = [begin
  +        Size = etorrent_io:piece_size(TorrentID, N)
  +        Set  = etorrent_chunkset:new(Size, 16#4000),
  +        {N, Set}
  +    end || N <- etorrent_pieceset:to_list(PiecesInvalid)],
  +
  +    %% Initialize an empty chunkset for all pieces that are valid.
  +    CompletedList = [begin
  +        Size = etorrent_io:piece_size(TorrentID, N),
  +        Set  = etorrent_chunkset:from_list(Size, 16#4000, []),
  +        {N, Set}
  +    end || N <- etorrent_pieceset:to_list(PiecesValid)],
  +
  +    AllChunksets = lists:sort(CompletedList ++ ChunkList),
  +    PieceChunks  = array:from_ordict(AllChunksets),
  +
  +    InitState = #state{
  +        pieces_valid=PiecesValid,
  +        pieces_chunked=PiecesChunked,
  +        piece_chunks=PieceChunks,
  +        peer_monitors=etorrent_monitorset:new()},
  +    {ok, InitState}.
  +
  +
  +handle_call({pick_chunks, Peerset, Numchunks}, _, State) ->
  +    #state{
  +        pieces_valid=PiecesValid,
  +        pieces_chunked=PiecesChunked,
  +        piece_chunks=PieceChunks} = State,
  +    
  +    %% If this peer has no pieces that we are already downloading, begin
  +    %% downloading a new piece if this peer has any interesting pieces.
  +    OptimalPieces    = etorrent_pieceset:intersection(Peerset, PiecesValid),
  +    SubOptimalPieces = etorrent_pieceset:difference(Peerset, PiecesValid),
  +    NumOptimal    = etorrent_pieceset:size(OptimalPieces),
  +    NumSubOptimal = etorrent_pieceset:size(SubOptimalPieces),
  +
  +    PieceIndex = case {NumOptimal, NumSubOptimal} of
  +        %% None that we are not already downloading
  +        %% and none that we would want to download
  +        {0, 0} ->
  +            none;
  +        %% None that we are not already downloading
  +        %% but one ore more that we would want to download
  +        {0, N} ->
  +            etorrent_pieceset:min(SubOptimalPieces);
  +        %% One or more that we are already downloading
  +        {N, _} ->
  +            etorrent_pieceset:min(OpimalPieces)
  +    end,
  +
  +    case PieceIndex of
  +        none ->
  +            {reply, {error, not_interested}, State};
  +        PieceIndex ->
  +            Chunkset    = array:get(PieceIndex, PieceChunks),
  +            {Offs, Len} = etorrent_chunkset:min(Chunkset),
  +            NewChunkset = etorrent_chunkset:delete(Offset, Len, Chunkset),
  +            NewChunks   = array:set(PieceIndex, NewChunkset, PieceChunks),
  +            NewChunked  = etorrent_pieceset:insert(PieceIndex, PiecesChunked),
  +            NewState = State#state{
  +                pieces_chunked=NewChunked,
  +                piece_chunks=NewChunks},
  +            {reply, {ok, [{PieceIndex, Offset, Len}]}, NewState}
  +    end;
  +
  +
   handle_call({mark_fetched, Id, Index, Offset, _Len}, _From, S) ->
       case ets:match_object(?TAB, #chunk { idt = {Id, Index, not_fetched},
  -					 chunk = {Offset, '_'} }) of
  +					 chunk = {Offset, '_', '_'} }) of
   	[] -> {reply, assigned, S};
   	[Obj] -> ets:delete_object(?TAB, Obj),
   		 {reply, found, S}
  
positive.code:     end.
  
  %% Check the piece Idx on torrent Id for completion
  check_piece(Id, Idx) ->
      _ = spawn_link(etorrent_io, check_piece_completion, [Id, Idx]),
      ets:match_delete(?TAB, #chunk { idt = {Id, Idx, '_'}, _ = '_'}).
  
  
negative.code:     RowsN = ets:select(?TAB, [{MatchHeadNotFetch, [], [{{'$1', '$2'}}]}]),
      Eligible = [{PN, Chunk} || {PN, Chunk} <- RowsA ++ RowsN,
                                  gb_sets:is_element(PN, PieceSet)],
      [{PN, Os, Sz, Ops} || {PN, {Os, Sz, Ops}} <- Eligible].
  
  %% @doc Chunkify a new piece.
  %%
  