Interim checkin - refactored further so https support can be added in smoothly.