Wrote unit tests and refactored.