integrate logic from pull request #26