Refactored and wrote some unit tests