Initial set of tests and minor refactoring