added validations and error handling