fix bugs, code cleanup