add a unit test and fix a bug