Custom implementation of pull request #435 by @shadone