Backport fix for #1018 and #1019 on 1.4.x