This replaces the dead pull request #2242. Thanks to @pniederlag for noticing the issue and providing the original PR.