Refactored and added unit tests