Added check if a Database is clean before freeing it