Refactored and added test suite