revised version of PR #154

rebased PR689 (which was originally @ekabadayi's pull request #689)

original message:
version of PR #154