Added more robust error checking

Added calls to import errno, also improved the error checking done in
the try: except: calls.