This reverts the temporary fix for #5534 (commit 99b6e75956 ) and implements a proper fix. This also closes #5570 as that was a side-effect of the initial half-assed fix.
99b6e75956