Fix the issue tkim found, and add a unit test.