Fixed some bugs and added new tests.