Added validations and error handling