fixed bugs, added tests