Backport #1872

Backport #1872, also fixes #3048 

@herbrechtsmeier @karlitschek