better input validation and error handling