Pick usefull commits from master plus some cleanups/fixes

Some are only in my master pull request.