issue #228 fix some bugs and provide unit test