fix bugs, and optimize