Improved the heartbeat monitoring logic. Fixed more bugs.