fix bugs & refactoring