fixed some bugs and wrote some new tests