Tls squashed

This squashes most commits from pull #864, see commit message for extensive description.