Adding more tests and other refactorings