Refactored some codes to be cleaner.