Some refactoring and adding configurable heartbeat timeout