Refactor client's main loop
Implement heartbeat