made error handling a bit more robust