Improve heartbeat function