Added tests, and fixed some bugs.