fix bugs and change some algorithm