Fixed serious bugs
Moved provider.close() to outer scope.
Merge branch 'task3' into task4