anchor.diff:  
   handle_call({request_chunks, PeerPid, Peerset, Numchunks}, _, State) ->
       #state{
  -        pieces_valid=PiecesValid,
  -        pieces_stored=PiecesStored,
  -        pieces_begun=PiecesChunked,
  -        piece_chunks=PieceChunks,
  +        pieces_valid=Valid,
  +        pieces_begun=Begun,
  +        pieces_assigned=Assigned,
  +        pieces_stored=Stored,
  +        chunks_assigned=AssignedChunks,
           peer_monitors=Peers} = State,
   
       %% Consider pieces that has an empty chunkset 
       
       %% 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, PiecesChunked),
  -    SubOptimalPieces = etorrent_pieceset:difference(Peerset, PiecesValid),
  -    NumOptimal    = etorrent_pieceset:size(OptimalPieces),
  -    NumSubOptimal = etorrent_pieceset:size(SubOptimalPieces),
  +    Optimal = etorrent_pieceset:intersection(Peerset,
  +              etorrent_pieceset:difference(Begun, Assigned)),
  +    SubOptimal = etorrent_pieceset:difference(Peerset, Assigned),
  +    NumOptimal = etorrent_pieceset:size(Optimal),
  +    NumSubOptimal = etorrent_pieceset:size(SubOptimal),
   
       ?debugVal({NumOptimal, NumSubOptimal}),
       PieceIndex = case {NumOptimal, NumSubOptimal} of
  
positive.code:     NewState = State#state{peer_monitors=NewPeers},
      {reply, ok, NewState};
  
  handle_call({mark_dropped, Pid, PieceIndex, Offset, Length}, _, State) ->
      #state{
          piece_chunks=PieceChunks,
          peer_monitors=Peers} = State,
       %% Reemove the chunk from the peer's set of open requests
       OpenReqs = etorrent_monitorset:fetch(Pid, Peers),
       NewReqs  = gb_trees:delete({PieceIndex, Offset}, OpenReqs),
       NewPeers = etorrent_monitorset:update(Pid, NewReqs, Peers),
   
       %% Add the chunk back to the chunkset of the piece.
       Chunks = array:get(PieceIndex, PieceChunks),
       NewChunks = etorrent_chunkset:insert(Offset, Length, Chunks),
       NewPieceChunks = array:set(PieceIndex, NewChunks, PieceChunks),
   
       NewState = State#state{
           piece_chunks=NewPieceChunks,
           peer_monitors=NewPeers},
      {reply, ok, NewState};
  
  handle_call({mark_all_dropped, Pid}, _, State) ->
      #state{
          piece_chunks=PieceChunks,
          peer_monitors=Peers} = State,
      %% Add the chunks back to the chunkset of each piece.
      OpenReqs = etorrent_monitorset:fetch(Pid, Peers),
      NewPieceChunks = lists:foldl(fun({PieceIndex, Offset, Length}, Acc) ->
          insert_chunk(PieceIndex, Offset, Length, Acc)
      end, PieceChunks, chunk_list(OpenReqs)),
      %% Replace the peer's set of open requests with an empty set
      NewPeers = etorrent_monitorset:update(Pid, gb_trees:empty(), Peers),
      NewState = State#state{
          piece_chunks=NewPieceChunks,
          peer_monitors=NewPeers},
      {reply, ok, NewState}.
  
  
  
  handle_cast(_, _) ->
      not_implemented.
  
  handle_info({'DOWN', _, process, Pid, _}, State) ->
      #state{
          piece_chunks=PieceChunks,
          peer_monitors=Peers} = State,
      %% Add the chunks back to the chunkset of each piece.
      OpenReqs = etorrent_monitorset:fetch(Pid, Peers),
      NewPieceChunks = lists:foldl(fun({PieceIndex, Offset, Length}, Acc) ->
          insert_chunk(PieceIndex, Offset, Length, Acc)
      end, PieceChunks, chunk_list(OpenReqs)),
      %% Delete the peer from the set of monitored peers
      NewPeers = etorrent_monitorset:delete(Pid, Peers),
      NewState = State#state{
          piece_chunks=NewPieceChunks,
          peer_monitors=NewPeers},
      {noreply, NewState}.
  
  
negative.code: -record(state, {
      torrent_id      :: pos_integer(),
      chunk_size      :: pos_integer(),
      %% Piece state sets
      pieces_valid    :: pieceset(),
      pieces_begun    :: pieceset(),
      pieces_assigned :: pieceset(),
      pieces_stored   :: pieceset(),
      %% Chunk sets for pieces
      chunks_assigned :: array(),
      chunks_stored   :: array(),
      %% Chunks assigned to peers
      peer_monitors   :: monitorset()}).
  
  %% # Open requests
  