fix some bugs and tested on more cases