     end
   end
 
+  def handle_call({:add_piece_index, index}, _from, {metadata, data}) do
+    if index >= 0 and !(Map.has_key?(data.pieces, index)) do
+      {:reply, {:ok, index}, {metadata, %TorrentData{data | pieces: Map.put(data.pieces, index, "found")}}}
+    else
+      {:reply, {:error, "invalid index"}, {metadata, data}}
+    end
+  end
+
+  def handle_call({:add_multi_pieces, lst}, _from, {metadata, data}) do
+    new_pieces = Enum.reduce(lst, %{}, fn(elem, acc)->
+      if elem >= 0 and !(Map.has_key?(acc, elem)) and !(Map.has_key?(data.pieces, elem)) do
+        %{acc | elem => "found"}
+      else
+        acc
+      end
+    end)
+    {:reply, {:ok, new_pieces}, {metadata, %TorrentData{data | pieces: Map.merge(data.pieces, new_pieces)}}}
+  end
+
+  def handle_call({:delete_piece_index, index}, _from, {metadata, data}) do
+    if index >= 0 and Map.has_key?(data.pieces, index) do
+      {:reply, {:ok, Map.fetch!(data.pieces, index)}, {metadata, %TorrentData{data | pieces: Map.delete(data.pieces, index)}}}
+    else
+      {:reply, {:error, "Invalid index"}, {metadata, data}}
+    end
+  end
+
   def handle_cast({:connect_to_tracker_async}, {metadata, data}) do
     {_, _, {new_metadata, new_data}} = connect_to_tracker_helper({metadata, data})
     {:noreply, {new_metadata, new_data}}

      }
    end
  end
end

      {:mark_piece_index_done, index})
  end

  def add_new_piece_index(id, index) do
    Logger.debug fn -> "#{id} is attempting to add new piece index: #{index}" end
    GenServer.call(:global.whereis_name({:btc_torrentworker, id}),
      {:add_piece_index, index})
  end

  def add_multi_pieces(id, lst) do
    Logger.debug fn -> "#{id} is attempting to add multliple pieces" end
    GenServer.call(:global.whereis_name({:btc_torrentworker, id}),
      {:add_piece_index, lst})
  end
  #-------------------------------------------------------------------------------
  # Utility Functions
  #-------------------------------------------------------------------------------
