Better error handling for invalid input