Some improvements in error handling