anchor.diff:      end.
   
   wait_for_msg(Handler, Mote, Msg) ->
  -    wait_for_msg(Handler, Mote, 1000, Msg).
  +    wait_for_msg(Handler, Mote, handler_timeout(Handler), Msg).
   
   wait_for_msg(_, _, T, _) when T=<0 -> throw(timeout);
   wait_for_msg(Handler, Mote, T, Msg) ->
  +    StepSize = handler_step_size(Handler),
       case state() of
   	{running, _} ->
   	    case mote_read(Handler, Mote) of
   		"" ->
  -		    ok = simulation_step(Handler, ?STEP_SIZE),
  -		    wait_for_msg(Handler, Mote, T-?STEP_SIZE, Msg);
  +		    ok = simulation_step(Handler, StepSize),
  +		    wait_for_msg(Handler, Mote, T-StepSize, Msg);
   		S ->
   		    case re:run(S, "DEBUG[^\n]*\n") of
   			{match, [{_,_}]} ->
  -			    ok = simulation_step(Handler, ?STEP_SIZE),
  +			    ok = simulation_step(Handler, StepSize),
   			    %% io:format("<<~p>>~n", [S]),
  -			    wait_for_msg(Handler, Mote, T-?STEP_SIZE, Msg);
  +			    wait_for_msg(Handler, Mote, T-StepSize, Msg);
   			_ ->
   			    case re:run(S, Msg) of
   				{match, _} ->
   				    true;
   				nomatch ->
  -				    ok = simulation_step(Handler, ?STEP_SIZE),
  -				    wait_for_msg(Handler, Mote, T-?STEP_SIZE, Msg)
  +				    ok = simulation_step(Handler, StepSize),
  +				    wait_for_msg(Handler, Mote, T-StepSize, Msg)
   			    end
   		    end
   	    end;
  
positive.code: 	    end
      end.
  
  quit_cooja(Handler) ->
      Handler ! {self(), quit_cooja},
      receive_answer(Handler).
  
  start_simulation(Handler) ->
      Handler ! {self(), start_simulation},
      receive_answer(Handler).
  
  stop_simulation(Handler) ->
      Handler ! {self(), stop_simulation},
      receive_answer(Handler).
  
  set_speed_limit(Handler, SpeedLimit) ->
      Handler ! {self(), set_speed_limit, {SpeedLimit}},
      receive_answer(Handler).
  
  set_random_seed(Handler, Seed) ->
      Handler ! {self(), set_random_seed, {Seed}},
      receive_answer(Handler).
  
  is_running(Handler) ->
      Handler ! {self(), is_running},
      receive_answer(Handler).
  
  simulation_time(Handler) ->
      Handler ! {self(), simulation_time},
      receive_answer(Handler).
  
  simulation_time_ms(Handler) ->
      Handler ! {self(), simulation_time_ms},
      receive_answer(Handler).
  
  simulation_step_ms(Handler) ->
      Handler ! {self(), simulation_step_ms},
      receive_answer(Handler).
  
  simulation_step(Handler, Time) ->
      Handler ! {self(), simulation_step, {Time}},
      receive_answer(Handler).
  
  radio_set_config(Handler, {Radio, Options}) ->
      Handler ! {self(), radio_set_config, {Radio, Options}},
      receive_answer(Handler).
  
  radio_get_config(Handler) ->
      Handler ! {self(), radio_get_config},
      receive_answer(Handler).
  
  radio_listen(Handler, Analyzers) ->
      Handler ! {self(), radio_listen, {Analyzers}},
      receive_answer(Handler).
      
  radio_unlisten(Handler) ->
      Handler ! {self(), radio_unlisten},
      receive_answer(Handler).
      
  radio_get_messages(Handler) ->
      Handler ! {self(), radio_get_messages},
      receive_answer(Handler).
  
  motes(Handler) ->
      Handler ! {self(), motes},
      receive_answer(Handler).
  
  mote_types(Handler) ->
      Handler ! {self(), mote_types},
      receive_answer(Handler).
  
  mote_add(Handler, Type) ->
      Handler ! {self(), mote_add, {Type}},
      receive_answer(Handler).
  
  mote_del(Handler, Id) ->
      Handler ! {self(), mote_del, {Id}},
      receive_answer(Handler).
  
  mote_get_pos(Handler, Id) ->
      Handler ! {self(), mote_get_pos, {Id}},
      receive_answer(Handler).
  
  mote_set_pos(Handler, Id, Pos) ->
      {X, Y, Z} = Pos,
      Handler ! {self(), mote_set_pos, {Id, X, Y, Z}},
      receive_answer(Handler).
  
  mote_write(Handler, Mote, Data) ->
      Handler ! {self(), mote_write, {Mote, Data}},
      receive_answer(Handler).
  
  mote_listen(Handler, Mote) ->
      Handler ! {self(), mote_listen, {Mote}},
      receive_answer(Handler).
  
  mote_unlisten(Handler, Mote) ->
      Handler ! {self(), mote_unliste, {Mote}},
      receive_answer(Handler).
  
  mote_hw_listen(Handler, Mote) ->
      Handler ! {self(), mote_hw_listen, {Mote}},
      receive_answer(Handler).
  
  mote_hw_unlisten(Handler, Mote) ->
      Handler ! {self(), mote_hw_unliste, {Mote}},
      receive_answer(Handler).
  
  mote_hw_events(Handler, Mote) ->
      Handler ! {self(), mote_hw_events, {Mote}},
      receive_answer(Handler).
  
  mote_read(Handler, Mote) ->
      Handler ! {self(), mote_read, {Mote}},
      receive_answer(Handler).
  
  mote_read_s(Handler, Mote) ->
      Handler ! {self(), mote_read_s, {Mote}},
      receive_answer(Handler).
  
  msg_wait(Handler, Msg) ->
      Handler ! {self(), msg_wait, {Msg}},
      receive_answer(Handler).
  
  get_last_event(Handler, Id) ->
      Handler ! {self(), get_last_event, {Id}},
      case  receive_answer(Handler) of
  	{not_running, E} -> {not_running, E};
  	no_event -> no_event;
  	R -> string:substr(R, 7, length(R)-7)
      end.
  
  %% high-level stuff
  next_event(Handler, Mote) ->
      next_event(Handler, Mote, 1000).
  
  next_event(_, _, T) when T=<0 -> throw(timeout);
  next_event(Handler, Mote, T) ->
      case get_last_event(Handler, Mote) of
  	not_listened_to ->
  	    fail;
  	badid ->
  	    badid;
  	no_event ->
  	    ok = simulation_step(Handler, ?STEP_SIZE),
  	    next_event(Handler, Mote, T-?STEP_SIZE);
  	E ->
  	    E
      end.
  
negative.code:     end.
  
  wait_for_result(Handler, Mote) ->
      wait_for_result(Handler, Mote, handler_timeout(Handler)).
  
  wait_for_result(_,_,T) when T=<0 -> throw(timeout);
  wait_for_result(Handler, Mote, T) ->
      StepSize = handler_step_size(Handler),
      case state() of
  	{running, _} ->
  	    case mote_read(Handler, Mote) of
  		"" ->
  		    ok = simulation_step(Handler, StepSize),
  		    wait_for_result(Handler, Mote, T-StepSize);
  		S ->
  		    case re:run(S, "DEBUG[^\n]*\n") of
  			{match, [{_,_}]} ->
  			    ok = simulation_step(Handler, StepSize),
  			    %% io:format("<<~p>>~n", [S]),
  			    wait_for_result(Handler, Mote, T-StepSize);
  			_ ->
  			    S
  		    end
  