def create_min_thickness_slab(structure,
                              index,
                              num_termination_min,
                              layerthickness,
                              layer_subtractions,
                              tol,
                              min_thickness=10):
    #Find final_slabthickness such that the cleaned slab is a least min_thickness thick after layer_subtractions times the average layerthickness
    #Cleaning consists of removing one layer on each side and then ensuring that the number of atomic layers is multiples of the number of unique terminations

    slabthickness = min_thickness
    final_slabthickness = 0
    while final_slabthickness < min_thickness + layerthickness * layer_subtractions:
        final_slab = SlabGenerator(structure,
                                   index,
                                   min_slab_size=slabthickness,
                                   min_vacuum_size=5,
                                   primitive=True,
                                   center_slab=True,
                                   in_unit_planes=False,
                                   max_normal_search=7).get_slab()
        final_slabthickness = np.max(final_slab.cart_coords[:, 2]) - np.min(
            final_slab.cart_coords[:, 2])
        slabthickness += 2  #increase slabthickness for potential next iteration

        if final_slabthickness > 5:  #Ensure that slab is thick enough for subtractions
            #"Clean" the two outside surfaces, pymatgen makes weird terminations sometimes
            min_value = np.min(final_slab.cart_coords[:, 2])
            first_layer_size = len(
                np.where(
                    np.abs(min_value - final_slab.cart_coords[:, 2]) < tol)[0])
            for i in range(first_layer_size):
                index_to_remove = np.argmin(final_slab.frac_coords[:, 2])
                final_slab.remove_sites([index_to_remove])

            max_value = np.max(final_slab.cart_coords[:, 2])
            first_layer_size = len(
                np.where(
                    np.abs(max_value - final_slab.cart_coords[:, 2]) < tol)[0])
            for i in range(first_layer_size):
                index_to_remove = np.argmax(final_slab.frac_coords[:, 2])
                final_slab.remove_sites([index_to_remove])

            nlayers = number_of_atomic_layers(final_slab, tol)

            #Delete all layers that are not multiple of num_termination_min (e.g. make ABCABCAB -> ABCABC; important for slabs with mirror symmetry)
            n_extra_layers = nlayers % num_termination_min
            if n_extra_layers != 0 and n_extra_layers < nlayers:
                for layer in range(n_extra_layers):
                    max_value = np.max(final_slab.cart_coords[:, 2])
                    first_layer_size = len(
                        np.where(
                            np.abs(max_value -
                                   final_slab.cart_coords[:, 2]) < tol)[0])
                    for i in range(first_layer_size):
                        index_to_remove = np.argmax(final_slab.frac_coords[:,
                                                                           2])
                        final_slab.remove_sites([index_to_remove])

            final_slabthickness = np.max(
                final_slab.cart_coords[:, 2]) - np.min(
                    final_slab.cart_coords[:, 2])

    while final_slabthickness - 1.3 * num_termination_min * layerthickness > min_thickness + layerthickness * layer_subtractions:
        for nterm in range(num_termination_min):
            max_value = np.max(final_slab.cart_coords[:, 2])
            first_layer_size = len(
                np.where(
                    np.abs(max_value - final_slab.cart_coords[:, 2]) < tol)[0])
            for i in range(first_layer_size):
                index_to_remove = np.argmax(final_slab.frac_coords[:, 2])
                final_slab.remove_sites([index_to_remove])

        final_slabthickness = np.max(final_slab.cart_coords[:, 2]) - np.min(
            final_slab.cart_coords[:, 2])
    return final_slab