Fixes to new issues caused by earlier refactoring, additional unit tests