Backport #1151 and #1154 from 3.0 branch