Adding tests, and refactoring