Code clarity improvements and better handling of SSL connection shutdown