fix bugs and optimize