Fix and Improve error handling