fixed some bugs, added tests