fix bugs, refactor