'Inner optimization loop.'

def optimize_inner(self, operations, app_label=None):

    new_operations = []
    for (i, operation) in enumerate(operations):
        for (j, other) in enumerate(operations[(i + 1):]):
            in_between = operations[(i + 1):((i + j) + 1)]
            result = operation.reduce(other, in_between, app_label)
            if isinstance(result, list):
                new_operations.extend(result)
                new_operations.extend(in_between)
                new_operations.extend(operations[((i + j) + 2):])
                return new_operations
            if (not result):
                new_operations.append(operation)
                break
        else:
            new_operations.append(operation)
    return new_operations
