refactored
refactored and added unit tests.