Moar bugfixes

Some non trivial, yet simple fixes for various bugs.

**Important:** This bugfixes touch important spots and especially for 17a6992, 7dce1ac and c56d635 they could introduce regressions or unwanted effects. I've tested various corner cases to make sure their behaviour stayed the same, but I can have missed something.

PS: It's pleasing to see how these bugfixes actually end in less code than before! :smiley_cat: There's more refactors coming, however.