ported changes from PR #2867