Close the database correctly