Improve error handling and fix a small bug.
New Feature: ssl support