Added some tests and fixed bugs.