Adding heartbeat code

Not tested.