Bringing branch 4.0.x up to date

Several changes were made to master relating to 4.0.0 release (some post release date) and these should have been applied to the 4.0.x branch but weren't. Applying now retrospectively.