Refactored code to avoid throwing Exception class, Simplified some codes