Fix error handling in script