Moved provider.close() to outer scope.