Code refactoring and reimplementation of heartbeat.