Refactored to support ssl