Added checks and error handling.
Refactored code.
Refactored code.