anchor.diff:  %%           process and figure out what to do with it. See if we
   %%           should send it on to our parent and record if this was a
   %%           final response (set State#state.completed=true if so).
  -%% Returns : NewState = state record()
  +%% Returns : State = state record() | does not return
   %%--------------------------------------------------------------------
   %%
   %% Method is INVITE and we are already completed
   %%
  -handle_sipproxy_response(Response, #state{sipmethod="INVITE", completed=true}=State) when record(Response, response) ->
  -    Request = State#state.request,
  +handle_sipproxy_response(Response, #state{request=Request, completed=true}=State)
  +  when is_record(Response, response), is_record(Request, request), Request#request.method == "INVITE" ->
       URI = Request#request.uri,
       {Status, Reason} = {Response#response.status, Response#response.reason},
       if
   	Status >= 200, Status =< 299 ->
  -	    %% XXX change to debug level
  +	    %% XXX change to debug level? Might be good to see in the log though, since
  +	    %% resends of the 200 Ok means that the ACK is not getting through.
   	    logger:log(normal, "Appserver glue: Forwarding 'late' 2xx response, ~p ~s to INVITE ~s statelessly",
   		       [Status, Reason, sipurl:print(URI)]),
   	    transportlayer:send_proxy_response(none, Response);
   	true ->
   	    logger:log(error, "Appserver glue: NOT forwarding non-2xx response ~p ~s to INVITE ~s - " ++
   		       "a final response has already been forwarded (sipproxy should not do this!)",
  -		       [Status, Reason, sipurl:print(URI)])
  +		       [Status, Reason, sipurl:print(URI)]),
  +	    erlang:error(more_than_one_final_response, [Response, State])
       end,
       State;
   %%
   %% Method is non-INVITE and we are already completed
   %%
  -handle_sipproxy_response(Response, #state{completed=true}=State) when record(Response, response) ->
  +handle_sipproxy_response(Response, #state{completed=true}=State) when is_record(Response, response) ->
       Request = State#state.request,
       {Method, URI} = {Request#request.method, Request#request.uri},
       {Status, Reason} = {Response#response.status, Response#response.reason},
       logger:log(error, "Appserver glue: NOT forwarding response ~p ~s to non-INVITE request ~s ~s - " ++
   	       "a final response has already been forwarded (sipproxy should not do this!)",
   	       [Status, Reason, Method, sipurl:print(URI)]),
  -    State;
  -%%
  -%% We are not yet completed
  -%%
  -handle_sipproxy_response(Response, State) when record(Response, response), record(State, state) ->
  +    erlang:error(more_than_one_final_response, [Response, State]);
  +
  +%%
  +%% We are not yet completed, this is not a provisional response, and we have a CPL-pid in State.
  +%% Forward response to the CPL-pid, so that it can take whatever action the CPL-script stipulates.
  +%%
  +handle_sipproxy_response(#response{status=Status}=Response, #state{cpl_pid=CPLpid}=State)
  +  when is_pid(CPLpid), Status >= 200 ->
  +    Request = State#state.request,
  +    {Method, URI} = {Request#request.method, Request#request.uri},
  +    {Status, Reason} = {Response#response.status, Response#response.reason},
  +    %% We always tell CallHandler about 2xx responses
  +    if
  +	Status >= 200, Status =< 299 ->
  +	    CallHandler = State#state.callhandler,
  +	    logger:log(debug, "Appserver glue: Forwarding response '~p ~s' to '~s ~s' to CallHandler ~p",
  +		       [Status, Reason, Method, sipurl:print(URI), CallHandler]),
  +	    transactionlayer:send_proxy_response_handler(CallHandler, Response);
  +	true -> true
  +    end,
  +    %% Now, tell CPLpid about the final response
  +    CPLpid = State#state.cpl_pid,
  +    logger:log(debug, "Appserver glue: Forwarding final response '~p ~s' to '~s ~s' to CPL-pid ~p",
  +	       [Status, Reason, Method, sipurl:print(URI), CPLpid]),
  +    CPLpid ! {appserver_glue_final_response, self(), Response},
  +    State#state{completed=true};
  +%%
  +%% We are not yet completed, and State does not contain a CPL-pid, or this is a provisional
  +%% response so we basically forward the response to our CallHandler, and if if it >= 200 we
  +%% set 'completed' to 'true'.
  +%%
  +handle_sipproxy_response(Response, State) when is_record(Response, response), is_record(State, state) ->
       Request = State#state.request,
       {Method, URI} = {Request#request.method, Request#request.uri},
       {Status, Reason} = {Response#response.status, Response#response.reason},
       CallHandler = State#state.callhandler,
       logger:log(debug, "Appserver glue: Forwarding response '~p ~s' to '~s ~s' to CallHandler ~p",
   	       [Status, Reason, Method, sipurl:print(URI), CallHandler]),
  -    case transactionlayer:is_good_transaction(CallHandler) of
  -	true ->
  -	    transactionlayer:send_proxy_response_handler(CallHandler, Response);
  -	false ->
  -	    logger:log(error, "Appserver glue: Can't forward response '~p ~s' to bad CallHandler '~p'",
  -		       [Status, Reason, CallHandler])
  -    end,
  +    transactionlayer:send_proxy_response_handler(CallHandler, Response),
       if
   	Status >= 200 ->
   	    State#state{completed=true};
   	true ->
   	    State
       end.
  -
  -%%--------------------------------------------------------------------
  -%% Function: garbage_collect_pid(Descr, Pid)
  -%%           Descr = string(), only used in logging
  -%%           Pid   = pid() | none
  -%% Descrip.: Check if a process is still alive, return 'none' if it
  -%%           isn't.
  -%% Returns : Pid | none
  -%%           Pid = pid(), same as input Pid
  -%%--------------------------------------------------------------------
  -garbage_collect_pid(_Descr, none) ->
  -    none;
  -garbage_collect_pid(Descr, Pid) when is_pid(Pid) ->
  -    case util:safe_is_process_alive(Pid) of
  -	{true, _}  ->
  -	    Pid;
  -	_ ->
  -	    logger:log(error, "Appserver glue: ~s ~p is not alive, garbage collected.",
  -		       [Descr, Pid]),
  -	    none
  -    end.
  -
  -%%--------------------------------------------------------------------
  -%% Function: garbage_collect_transaction(Descr, TH)
  -%%           Descr = string(), only used in logging
  -%%           TH    = term() (thandler record())
  -%% Descrip.: Check if a transaction handle still refers to an alive
  -%%           transaction handler process.
  -%% Returns : Pid | none
  -%%           Pid = pid(), same as input Pid
  -%%--------------------------------------------------------------------
  -garbage_collect_transaction(Descr, TH) ->
  -    case transactionlayer:is_good_transaction(TH) of
  -	true ->
  -	    TH;
  -	_ ->
  -	    logger:log(error, "Appserver glue: ~s transaction is not alive, garbage collected.",
  -		       [Descr]),
  -	    none
  -    end.
  
positive.code: 
  %%--------------------------------------------------------------------
  %% Function: init([Request, Actions])
  %%           Request     = request record()
  %%           Actions     = list() of sipproxy_action record()
  %% Descrip.: Initiates the server
  %% Returns : {ok, State, Timeout} |
  %%           error (parent will throw a siperror (500))
  %%--------------------------------------------------------------------
  init([Request, Actions]) ->
      case adopt_and_get_branchbase(Request) of
  	{ok, CallHandler, BranchBase} ->
  	    Method = {Request#request.method},
  	    ForkPid = sipserver:safe_spawn(appserver, start_actions, [BranchBase, self(), Request, Actions]),
  	    logger:log(normal, "~s: Appserver glue: Forking request, ~p actions",
  		       [BranchBase, length(Actions)]),
  	    logger:log(debug, "Appserver glue: Forking request, glue process ~p, CallHandler ~p, ForkPid ~p",
  		       [self(), CallHandler, ForkPid]),
  	    %% We need the pid of the callhandler extracted to do guard matches on it
  	    CHPid = transactionlayer:get_pid_from_handler(CallHandler),
  	    logger:log(debug, "Appserver glue started"),
  	    {ok, #state{branchbase=BranchBase, callhandler=CallHandler, forkpid=ForkPid,
  			request=Request, actions=Actions, callhandler_pid=CHPid, sipmethod=Method},
  	     ?TIMEOUT};
  	Res ->
  	    Res
      end.
  
  %%--------------------------------------------------------------------
  %% Function: adopt_and_get_branchbase(Request)
  %%           Request = request record()
  %% Descrip.: As part of initialization, we adopt the server
  %%           transaction, and fetch it's generated branch. We then
  %%           remove the "-UAS" suffix from the branch so that we can
  %%           generate easily correlatable client branches.
  %% Returns : {ok, CallHandler, BranchBase} |
  %%           error |
  %%           ok
  %%--------------------------------------------------------------------
  adopt_and_get_branchbase(Request) when is_record(Request, request) ->
      URI = Request#request.uri,
      case transactionlayer:adopt_server_transaction(Request) of
  	{error, cancelled} ->
  	    logger:log(normal, "Appserver: Request '~s ~s' has allready been cancelled, aborting.",
  		       [Request#request.method, sipurl:print(URI)]),
  	    ok;
  	{error, E} ->
  	    logger:log(error, "Appserver: Failed to adopt server transaction for request ~s ~s : ~p",
  		       [Request#request.method, sipurl:print(URI), E]),
  	    error;
  	CallHandler ->
  	    CallBranch = transactionlayer:get_branch_from_handler(CallHandler),
  	    case string:rstr(CallBranch, "-UAS") of
  		0 ->
  		    logger:log(error, "Appserver: Could not make branch base from branch ~p", [CallBranch]),
  		    error;
  		Index when integer(Index) ->
  		    BranchBase = string:substr(CallBranch, 1, Index - 1),
  		    {ok, CallHandler, BranchBase}
  	    end
      end.
  
  %%--------------------------------------------------------------------
  %% Function: handle_call(Msg, From, State)
  
negative.code: 		       [BranchBase, Method, sipurl:print(URI)]),
  	    logger:log(debug, "Appserver glue: Abnormally exiting with reason :~n~p", [Reason])
      end,
      Reason.
  
  %%--------------------------------------------------------------------
  %% Function: code_change(OldVsn, State, Extra)
  %% Descrip.: Convert process state when code is changed
  %% Returns : {ok, NewState}
  %%--------------------------------------------------------------------
  code_change(_OldVsn, State, _Extra) ->
  