fixed some bugs, added some tests