Solved some bugs, more tests are needed