fix bug and add unit test