Added some tests. Fixed some bugs.