This is a reversion of the reversion of commit c1925952, after more discussion on the issue. Fixes: #1258