Refactor heartbeat to remove race