Additional backports for bugfix release 4.0.6.

Backporting #697 to `4.0-stable` branch.

@deiferni