Refactored some codes