Fixed some bugs and tests