Added unit tests and fixes.