Added Unit tests. Bugs fixed.