refactor, fix bugs