Cherry-picked changes from pull request #45.