Updated with Struan's fixes for https.