Added some tests and refactored code