defect fixes and better error handling