Added new tests and fixes some bugs