anchor.diff:  handle_call({get_proc, Lang}, From, State) ->
       {ClientPid, _} = From,
       IterFun = fun(Proc, _Acc) ->
  -        {stop, assign_proc(State#state.tab, ClientPid, Proc)}
  +        {stop, assign_proc(ClientPid, Proc)}
       end,
       Client = #client{from=From, lang=couch_util:to_binary(Lang)},
       find_proc(State, Client, [IterFun]);
   
  -handle_call({ret_proc, #proc{client=Ref, lang=Lang0} = Proc}, _From, State) ->
  +handle_call({ret_proc, #proc{client=Ref} = Proc}, _From, State) ->
       erlang:demonitor(Ref, [flush]),
  -    Lang = couch_util:to_binary(Lang0),
  -    % We need to check if the process is alive here, as the client could be
  -    % handing us a #proc{} with a dead one.  We would have already removed the
  -    % #proc_int{} from our own table, so the alternative is to do a lookup in the
  -    % table before the insert.  Don't know which approach is cheaper.
  -    {reply, true, return_proc(State, Proc#proc{lang=Lang})};
  -
  -handle_call(bump_threshold_ts, _From, #state{tab = Tab} = State) ->
  -    FoldFun = fun(#proc_int{client = nil, pid = Pid}, _) ->
  -        remove_proc(Tab, Pid);
  -    (_, _) ->
  -        ok
  -    end,
  -    ets:foldl(FoldFun, nil, Tab),
  -    {reply, ok, State#state{threshold_ts = os:timestamp()}};
  -
  -handle_call(terminate_stale_procs, _From, State) ->
  -    #state{tab = Tab, threshold_ts = T0} = State,
  -    MatchHead = #proc_int{pid = '$1', t0 = '$2', _ = '_'},
  -    MatchSpec = [{MatchHead, [{'<', '$2', T0}], ['$1']}],
  -    lists:foreach(fun(P) -> remove_proc(Tab,P) end, ets:select(Tab, MatchSpec)),
  -    {reply, ok, State};
  +    NewState = case ets:lookup(?PROCS, Proc#proc.pid) of
  +        [#proc_int{}=ProcInt] ->
  +            return_proc(State, ProcInt);
  +        [] ->
  +            % Proc must've died and we already
  +            % cleared it out of the table in
  +            % the handle_info clause.
  +            State
  +    end,
  +    {reply, true, NewState};
  +
  +handle_call(set_threshold_ts, _From, State) ->
  +    FoldFun = fun
  +        (#proc_int{client = undefined} = Proc, StateAcc) ->
  +            remove_proc(StateAcc, Proc);
  +        (_, StateAcc) ->
  +            StateAcc
  +    end,
  +    NewState = ets:foldl(FoldFun, State, ?PROCS),
  +    {reply, ok, NewState#state{threshold_ts = os:timestamp()}};
  +
  +handle_call(terminate_stale_procs, _From, #state{threshold_ts = Ts1} = State) ->
  +    FoldFun = fun
  +        (#proc_int{client = undefined, t0 = Ts2} = Proc, StateAcc) ->
  +            case Ts1 > Ts2 of
  +                true ->
  +                    remove_proc(StateAcc, Proc);
  +                false ->
  +                    StateAcc
  +            end;
  +        (_, StateAcc) ->
  +            StateAcc
  +    end,
  +    NewState = ets:foldl(FoldFun, State, ?PROCS),
  +    {reply, ok, NewState};
   
   handle_call(_Call, _From, State) ->
       {reply, ignored, State}.
   
  -handle_cast({os_proc_idle, Pid}, #state{tab=Tab, proc_counts=Counts}=State0) ->
  -    Limit = list_to_integer(
  -            config:get("query_server_config", "os_process_soft_limit", "100")),
  -    State = case ets:lookup(Tab, Pid) of
  -        [#proc_int{client=nil, lang=Lang}] ->
  +
  +handle_cast({os_proc_idle, Pid}, #state{counts=Counts}=State) ->
  +    NewState = case ets:lookup(?PROCS, Pid) of
  +        [#proc_int{client=undefined, lang=Lang}=Proc] ->
               case dict:find(Lang, Counts) of
  -                {ok, Count} when Count > Limit ->
  +                {ok, Count} when Count >= State#state.soft_limit ->
                       ?LOG_INFO("Closing idle OS Process: ~p", [Pid]),
  -                    remove_proc(Tab, Pid),
  -                    State0#state{
  -                        proc_counts=dict:update_counter(Lang, -1, Counts)
  -                    };
  +                    remove_proc(State, Proc);
                   {ok, _} ->
  -                    State0
  +                    State
               end;
           _ ->
  -            State0
  -    end,
  -    {noreply, State};
  +            State
  +    end,
  +    {noreply, NewState};
  +
   handle_cast(reload_config, State) ->
  -    {noreply, State#state{config = get_proc_config()}};
  +    NewState = State#state{
  +        config = get_proc_config(),
  +        hard_limit = get_hard_limit(),
  +        soft_limit = get_soft_limit()
  +    },
  +    {noreply, flush_waiters(NewState)};
  +
   handle_cast(_Msg, State) ->
       {noreply, State}.
   
  
positive.code:     TeachFun = fun(Proc0, Acc) ->
          try
              {ok, Proc1} = teach_ddoc(DDoc, DDocKey, Proc0),
              {stop, assign_proc(State#state.tab, ClientPid, Proc1)}
          catch _:_ ->
              {ok, Acc}
          end
  
negative.code: find_proc(State, Client, []) ->
      {noreply, maybe_spawn_proc(State, Client)}.
  
  
  iter_procs(Lang, Fun, Acc) when is_binary(Lang) ->
      Pattern = #proc_int{lang=Lang, client=undefined, _='_'},
      MSpec = [{Pattern, [], ['$_']}],
      case ets:select_reverse(?PROCS, MSpec, 25) of
          '$end_of_table' ->
              {not_found, Acc};
          Continuation ->
              iter_procs_int(Continuation, Fun, Acc)
      end.
  
  
  iter_procs_int({[], Continuation0}, Fun, Acc) ->
      case ets:select_reverse(Continuation0) of
          '$end_of_table' ->
              {not_found, Acc};
          Continuation1 ->
              iter_procs_int(Continuation1, Fun, Acc)
      end;
  iter_procs_int({[Proc | Rest], Continuation}, Fun, Acc0) ->
      case Fun(Proc, Acc0) of
          {ok, Acc1} ->
              iter_procs_int({Rest, Continuation}, Fun, Acc1);
          {stop, Acc1} ->
              {ok, Acc1}
      end.
  
  
  maybe_spawn_proc(State, Client) ->
      case dict:find(Client#client.lang, State#state.counts) of
      {ok, Count} when Count >= State#state.hard_limit ->
          add_waiting_client(Client),
          State;
      _ ->
          spawn_proc(State, Client)
      end.
  
  
  spawn_proc(State, Client) ->
      Pid = spawn_link(?MODULE, new_proc, [Client]),
      ets:insert(?OPENING, {Pid, Client}),
      Counts = State#state.counts,
      Lang = Client#client.lang,
      State#state{
          counts = dict:update_counter(Lang, 1, Counts)
      }.
  
  
  new_proc(#client{ddoc=undefined, ddoc_key=undefined}=Client) ->
      #client{from=From, lang=Lang} = Client,
      Resp = try
          case new_proc_int(From, Lang) of
              {ok, Proc} ->
                  {spawn_ok, Proc, From};
              Error ->
                  gen_server:reply(From, {error, Error}),
                  spawn_error
          end
      catch _:_ ->
          spawn_error
      end,
      exit(Resp);
  
  new_proc(Client) ->
      #client{from=From, lang=Lang, ddoc=DDoc, ddoc_key=DDocKey} = Client,
      Resp = try
          case new_proc_int(From, Lang) of
          {ok, NewProc} ->
              case teach_ddoc(DDoc, DDocKey, NewProc) of
              {ok, Proc} ->
                  {spawn_ok, Proc, From};
              {error, Reason} ->
                  gen_server:reply(From, {error, Reason}),
                  spawn_error
              end;
          Error ->
              gen_server:reply(From, {error, Error}),
              spawn_error
          end
      catch _:_ ->
          spawn_error
      end,
      exit(Resp).
  
  
  new_proc_int(From, Lang) when is_binary(Lang) ->
      LangStr = binary_to_list(Lang),
      case config:get("query_servers", LangStr) of
      undefined ->
          case config:get("native_query_servers", LangStr) of
          undefined ->
              gen_server:reply(From, {unknown_query_language, Lang});
          SpecStr ->
  