[TASK] Numerous bugfixes, cleanups and test improvements

Bug fixes explicitly committed, the rest is in bulk mode.