More refactoring - getting unit tests working - almost!