More code cleanup

Including minor bugfixes; see commit messages