Remove some bugs andoptimize performance