More bug fixes.
I hate leaving this unfinished but I really need to go to bed...