revised version of PR #154

revised version of PR #154 (which was originally @ekabadayi's pull request #689)