Backporting fixes from master branch.