refactoring some codes