Improve error message and handling