Added QUnit tests and fixed bugs