Added a few tests and refactoring