Backport two important changes to 0.21 branch

These are IMO quite important improvements in upstream but since there is no 0.22 release yet it would be good to have them in the 0.21 branch too (and perhaps a new release/tag of the 0.21 branch could be made).

These are cherry-picked commits e08365777199901d58e7cf4cc269ffabd07b2659 and d6ecc311408b1042bb175aa0027b48b6224bc571.