Fixed some bugs, tests pass