Backport fixes from master branch