HTTPS-aware and some cleanup