Improve error handling