Completed input checking and refactored some codes