Made some code improvements and ensured that the echo thread will always quit