Fix some bugs.
Add test.