Backport some things from master.