Add heartbeat function, some bug fixed