Reimplementation of bc80d0c and f2e6122, because they have to be reverted by 0b9a8aa.
bc80d0c
f2e6122
0b9a8aa