Refactored and written unit tests.