Add more tests. Fix some bugs.