fix bugs and tested by unittest