Add some tests. Fix some bugs.