Modularize to use stand alone application.
Fix some bugs on connection handling. Improve error handling.