some refactoring bugfixes, part 1 of many to come