Added graceful handling of exceptions.