added tests and fixed some bugs