Fixes #967 WIP to be integrated with work from hudsonfoo in PR #1164 (cherry picked from commit ebadad61c6)
ebadad61c6