Fixed some bugs, added tests.