Fixing a few rather nasty bugs.