Revert "Lots of cleanup and minor bugfixes all over the code"

This reverts commit 0445ad9c25db8b9bf70d5298eeacbcb65c855bbd.