Code cleanup and some better error handling in SSL routines