refactor some codes