anchor.diff:      end.
   
   notify_status_change(Status, #state{name=Name,observers=Observers}) ->
  -    catch throw(whereami),
  -    Stack = erlang:get_stacktrace(),
  -    ?debugVal(Stack),
       lager:info("Status change for ~s : ~s", [Name, Status]),
       [ Observer ! { node_heartbeat_event, Name, Status } || Observer <- Observers ].
   
  
positive.code:             gen_fsm:send_all_state_event(Pid, {Action, self()})
      end.
  
  
  init([Name, HeartbeatInterval, DeadIntervalCount]) ->
      {ok, initializing,
       #state{dead_interval=HeartbeatInterval * DeadIntervalCount,
              name=Name,
              heartbeat_rate=eavg_init(DeadIntervalCount, HeartbeatInterval)
             },
       0}.
  
  initializing(timeout, #state{name=Name}=State) ->
      case gproc:reg({n, l, Name}) of
          true ->
              {next_state, down, save_status(?SAVE_MODE, down, State)};
          false ->
              lager:error("Failed to register:~p for ~p", [Name,self()]),
              {stop, shutdown, State}
      end.
  
  
  %%%
  %%% State machine internals
  %%%
  up(current_state, _From, State) ->
      {reply, up, up, State}.
  
  crashed(current_state, _From, State) ->
      {reply, crashed, crashed, State}.
  
  restarting(current_state, _From, State) ->
      {reply, restarting, restarting, State}.
  
  %%
  %% These events are handled the same everywhere
  %%
  handle_event({start_watching, Who}, StateName, #state{observers=Observers}=State) ->
      State1 = case lists:member(Who, Observers) of
          false ->
  
negative.code: handle_event({logging, Level}, StateName, State) ->
      State1 = State#state{logging=Level},
      {next_state, StateName, State1};
  handle_event(Event, StateName, #state{name=Name}=State) ->
      lager:error("FSM for ~p received unexpected event ~p", [Name, Event]),
      {next_state, StateName, State}.
  
  handle_sync_event(Event, _From, StateName, #state{name=Name}=State) ->
      lager:error("FSM for ~p received unexpected sync event ~p", [Name, Event]),
      {reply, ignored, StateName, State}.
  
  %%
  %% Handle info
  %%
  handle_info({'DOWN', _MonitorRef, _Type, Object, _Info}, CurState, State) ->
      Observers = State#state.observers,
      State1 = State#state{observers=lists:delete(Object,Observers)},
      {next_state, CurState, State1};
  handle_info({heartbeat, NodeName, NodeStatus},
              CurState,
              #state{heartbeats=HeartBeats, logging=Level, current_status=CurStatus, heartbeat_rate=HRate}=State) ->
      nlog(Level, "Heartbeat recieved from ~p Currently ~p ~p", [NodeName, NodeStatus, HRate]),
  
      %% Note that we got a heartbeat
      State1 = State#state{heartbeat_rate=push_ema:inc(HRate,1), current_status=NodeStatus},
      %% Reset the timer
      State2 = reset_timer(State1),
  
      case HeartBeats of
          ?POC_HB_THRESHOLD - 1 -> % transitioning up
              update_status(?SAVE_MODE, NodeStatus, State2),
              {next_state, up, State2#state{heartbeats=HeartBeats + 1}};
          ?POC_HB_THRESHOLD -> % up, only do something if status changes
              case NodeStatus of
                  CurStatus -> ok;
                  _Else -> update_status(?SAVE_MODE, NodeStatus, State2)
              end,
              {next_state, up, State2};
          _ -> % we're down, don't save state changes
              {next_state, CurState, State2#state{heartbeats=HeartBeats +1}}
      end;
  handle_info(down, down, State) ->
      {next_state, down, State};
  handle_info(down, _CurState, State) ->
      {next_state, down, update_status(?SAVE_MODE, down, State#state{heartbeats=0})};
  handle_info({timeout, _Ref, update_avg}, CurState, #state{heartbeat_rate=HRate}=State) ->
      {next_state, CurState, State#state{heartbeat_rate=push_ema:tick(HRate)} };
  handle_info(no_heartbeats, _CurState, State) ->
      State1 = State#state{heartbeats=0, tref=undefined},
      update_status(?SAVE_MODE, down, State1),
      {next_state, down, State1};
  handle_info(Info, CurState, State) ->
      lager:error("Strange message ~p received in state ~p", [Info, CurState]),
      {next_state, CurState, State}.
  
  
  terminate(_Reason, _CurState, _State) ->
      ok.
  
  code_change(_OldVsn, CurState, State, _Extra) ->
      {ok, CurState, State}.
  
  %% Internal functions
  
  create_status_record(direct, Status, State) ->
      notify_status_change(Status, State),
      update_status_directly(Status, State);
  create_status_record(gen_server, Status, #state{name=Name}=State) ->
      notify_status_change(Status, State),
      pushy_node_status_updater:create(?POC_ORG_ID, Name, ?POC_ACTOR_ID, Status),
      State.
  
  update_status(direct, Status, State) ->
      notify_status_change(Status, State),
      update_status_directly(Status, State);
  update_status(gen_server, Status, #state{name=Name}=State) ->
      notify_status_change(Status, State),
      pushy_node_status_updater:update(?POC_ORG_ID, Name, ?POC_ACTOR_ID, Status),
      State.
  
  update_status_directly(Status, #state{name=Name}=State) ->
      NodeStatus = pushy_object:new_record(pushy_node_status,
                                           ?POC_ORG_ID,
                                           [{<<"node">>, Name},{<<"type">>, Status}]),
      %% This probably should be refactored into a 'init_status' and a 'update_status'; once the
      %% FSM is started we should be able to assume the object exists, and go straight for update..
      case pushy_object:create_object(create_node_status, NodeStatus, ?POC_ACTOR_ID) of
          {ok, _} ->
  