Properly close database