Fixed some issues. Added unit tests.