Add test and fix bug