fixed some bugs and added tests