Fixed some bugs, added more tests