wrote unit tests and refactored a little