add unit test and fix bug