Refactor some more
Fix tests