Whooole bunch of bug fixes. This thing almost works now. Of course, I still need to check what happens when I switch AD on...