Refactor the heartbeat stuff