anchor.diff:      {stop, {error, {unexpected_call, Msg}}, State}.
   
   
  -handle_cast(rep_db_changed, State) ->
  -    #state{
  -        changes_feed_loop = Loop,
  -        changes_queue = Queue
  -    } = State,
  -    catch unlink(Loop),
  -    catch exit(Loop, rep_db_changed),
  -    couch_work_queue:queue(Queue, stop_all_replications),
  -    {ok, NewLoop} = changes_feed_loop(Queue),
  -    {noreply, State#state{changes_feed_loop = NewLoop}};
  -
  -handle_cast(rep_db_created, #state{changes_feed_loop = Loop} = State) ->
  -    catch unlink(Loop),
  -    catch exit(Loop, rep_db_changed),
  -    {ok, NewLoop} = changes_feed_loop(State#state.changes_queue),
  -    {noreply, State#state{changes_feed_loop = NewLoop}};
  +handle_cast({rep_db_changed, NewName},
  +        #state{rep_db_name = NewName} = State) ->
  +    {noreply, State};
  +
  +handle_cast({rep_db_changed, _NewName}, State) ->
  +    {noreply, restart(State)};
  +
  +handle_cast({rep_db_created, NewName},
  +        #state{rep_db_name = NewName} = State) ->
  +    {noreply, State};
  +
  +handle_cast({rep_db_created, _NewName}, State) ->
  +    {noreply, restart(State)};
   
   handle_cast(Msg, State) ->
       ?LOG_ERROR("Replicator DB listener received unexpected cast ~p", [Msg]),
       {stop, {error, {unexpected_cast, Msg}}, State}.
   
  +
   handle_info({'EXIT', From, normal}, #state{changes_feed_loop = From} = State) ->
       % replicator DB deleted
  -    couch_work_queue:queue(State#state.changes_queue, stop_all_replications),
  -    {noreply, State#state{changes_feed_loop = nil}};
  +    {noreply, State#state{changes_feed_loop = nil, rep_db_name = nil}};
   
   handle_info({'EXIT', From, Reason}, #state{db_notifier = From} = State) ->
       ?LOG_ERROR("Database update notifier died. Reason: ~p", [Reason]),
       {stop, {db_update_notifier_died, Reason}, State};
   
  -handle_info({'EXIT', From, Reason}, #state{changes_processor = From} = State) ->
  -    ?LOG_ERROR("Replicator DB changes processor died. Reason: ~p", [Reason]),
  -    {stop, {rep_db_changes_processor_died, Reason}, State}.
  +handle_info({'EXIT', From, normal}, #state{rep_start_pids = Pids} = State) ->
  +    % one of the replication start processes terminated successfully
  +    {noreply, State#state{rep_start_pids = Pids -- [From]}};
  +
  +handle_info(Msg, State) ->
  +    ?LOG_ERROR("Replicator DB listener received unexpected message ~p", [Msg]),
  +    {stop, {unexpected_msg, Msg}, State}.
   
   
   terminate(_Reason, State) ->
       #state{
  +        rep_start_pids = StartPids,
           changes_feed_loop = Loop,
  -        changes_queue = Queue
  +        db_notifier = Notifier
       } = State,
  -    exit(Loop, stop),
  -    % closing the queue will cause changes_processor to shutdown
  -    couch_work_queue:close(Queue),
  -    ok.
  +    stop_all_replications(),
  +    lists:foreach(
  +        fun(Pid) ->
  +            catch unlink(Pid),
  +            catch exit(Pid, stop)
  +        end,
  +        [Loop | StartPids]),
  +    true = ets:delete(?REP_ID_TO_DOC_ID),
  +    true = ets:delete(?DOC_ID_TO_REP_ID),
  +    couch_db_update_notifier:stop(Notifier).
   
   
   code_change(_OldVsn, State, _Extra) ->
       {ok, State}.
   
   
  -changes_feed_loop(ChangesQueue) ->
  +changes_feed_loop() ->
       {ok, RepDb} = couch_rep:ensure_rep_db_exists(),
  +    Server = self(),
       Pid = spawn_link(
           fun() ->
               ChangesFeedFun = couch_changes:handle_changes(
  
positive.code:         end
      ),
      couch_db:close(RepDb),
      {ok, Pid}.
  
  
  db_update_notifier() ->
  
negative.code: 
  -include("couch_db.hrl").
  
  -define(DOC_ID_TO_REP_ID, rep_doc_id_to_rep_id).
  -define(REP_ID_TO_DOC_ID, rep_id_to_rep_doc_id).
  
  -record(state, {
      changes_feed_loop = nil,
      db_notifier = nil,
      rep_db_name = nil,
      rep_start_pids = []
  }).
  
  -import(couch_util, [
      get_value/2,
      get_value/3
  ]).
  
  
  start_link() ->
  