Minor bugfixes and some straightening up of the code.