manually applying patch from pull request #4776