fix bugs, optimization