Preliminary support for TLS-secured connections, did some code clean-up