Backport of ticket #3030 and #3031

Should match PR #1122