Added tests and refactored