refactor heartbeat code