Further improve error handling