fixed and improved error handling