refactoring, fix bugs