added a bunch of tests and refactorings