Refactoring code for SSL support