Fix error-handling bugs