Quite a bunch of changes, most of which are for debugging and never should make it into a pull request. Still, pushed here so the curious can have a look.