More code cleanup

Including minor bugfixes; see commit comments