Import and upgrade patch from pull request #603