backporting PR #335