Improve error handling and error messaging