improve some performance and fix bugs