and some more refactoring. Still need to setup tests