please optimize this code and make it faster and more professional and solve potential bugs: private void RunScript(Brep srf, List<double> start, List<double> end, int count, double dist, double angle, int towards, ref object A, ref object B)
  {
    var surf = srf.Surfaces[0];

    Point3d origin = surf.PointAt(start[0], start[1]);
    Vector3d xAxis = surf.PointAt(end[0], end[1]) - origin;
    Vector3d yAxis = surf.NormalAt(start[0], start[1]);

    // Create a plane at the start point
    Plane plane;
    surf.FrameAt(start[0], start[1], out plane);
    Vector3d adjustedXAxis = plane.ClosestPoint(origin + xAxis) - origin;

    // Rotate the yAxis if angle is provided
    if (angle != 0.0)
      yAxis.Rotate(RhinoMath.ToRadians(angle), adjustedXAxis);

    plane = new Plane(origin, adjustedXAxis, yAxis);

    // Adjust plane if angle is 90 degrees
    if (angle == 90)
      plane.Origin += surf.NormalAt(start[0], start[1]) * 0.001;

    // Define curves and points outside the function
    Curve[] initialCurves;
    Point3d[] points;

    // Perform intersection of Brep with the plane
    if (!Rhino.Geometry.Intersect.Intersection.BrepPlane(srf, plane, 0.0001, out initialCurves, out points) || initialCurves.Length == 0)
      return;

    List<Curve> crvList = new List<Curve>(initialCurves); // Start with initial intersection curves

    // Handle different 'towards' directions
    switch (towards)
    {
      case 1:
        if (initialCurves[0].IsValid)
        {
          crvList.AddRange(GenerateCurvesParallel(initialCurves, srf, dist, count));
        }
        break;

      case 2:
        plane.Flip();
        Rhino.Geometry.Intersect.Intersection.BrepPlane(srf, plane, 0.0001, out initialCurves, out points);
        if (initialCurves.Length > 0 && initialCurves[0].IsValid)
        {
          crvList.AddRange(GenerateCurvesParallel(initialCurves, srf, dist, count));
        }
        break;

      case 3:
        if (initialCurves[0].IsValid)
        {
          crvList.AddRange(GenerateCurvesParallel(initialCurves, srf, dist, count));
          plane.Flip();
          Rhino.Geometry.Intersect.Intersection.BrepPlane(srf, plane, 0.0001, out initialCurves, out points);
          if (initialCurves.Length > 0 && initialCurves[0].IsValid)
          {
            crvList.AddRange(GenerateCurvesParallel(initialCurves, srf, dist, count));
          }
        }
        break;
    }

    A = crvList;
    B = plane; // Optionally return the plane

  }

  // <Custom additional code> 
  private List<Curve> GenerateCurvesParallel(Curve[] initialCurves, Brep srf, double dist, int count)
  {
    List<Curve> crvList = new List<Curve>();
    List<Curve> currentCurves = new List<Curve>(initialCurves); // Use all initial curves

    for (int i = 0; i < count; i++)
    {
      List<Curve> nextCurves = new List<Curve>();

      // Parallelize offset processing for all current curves
      System.Threading.Tasks.Parallel.ForEach(currentCurves, (curve) =>
        {
        var offsetCurves = curve.OffsetOnSurface(srf.Faces[0], dist, 0.00001);

        if (offsetCurves == null || offsetCurves.Length == 0)
          return;

        foreach (var offsetCurve in offsetCurves)
        {
          if (!offsetCurve.IsValid)
            continue;

          // Self-intersection check (for both open and closed curves)
          if (Rhino.Geometry.Intersect.Intersection.CurveSelf(offsetCurve, 0.1).Count > 0)
            continue;

          // If the curve is open, extend it on the surface
          Curve extendedCurve = offsetCurve;
          if (!extendedCurve.IsClosed)
          {
            CurveEnd side = CurveEnd.Both;
            extendedCurve = extendedCurve.ExtendOnSurface(side, srf.Faces[0]);
          }

          // Add to the next curve list in a thread-safe manner
          lock (nextCurves)
          {
            nextCurves.Add(extendedCurve);
          }
        }
        });

      // If no new valid curves were generated, break early
      if (nextCurves.Count == 0)
        break;

      crvList.AddRange(nextCurves);  // Add all the new curves
      currentCurves = nextCurves;    // Update the current curves for the next iteration
    }

    return crvList;
  }