Adding unit tests and refactored a little.