refactored some codes