Adding more detailed tests and refactoring