fixed a few bugs, added tests