written tests and fix some bugs