Fix bug in heartbeat thread