Some minor refactoring, small bugfixes