Added QUnit tests. Fixed some bugs.