Updated to add validations and error handling.