make code refactoring, add some minor fixes