Added unit testing and cleaned up code.