Add unit test; refactored code; fix logic
Add unit tests; refactored code