Close if correctly to avoid leaving open function.