4.0

Tracking changes from 4.x branch from 3.x.