refactoring, fix some flaws