Fixed tests and some bugs