Add test and fix bugs.