Added more input data validation and error handling