Python does not work consistent

Hi.
I have a bunch of python scripts id numerous dynamo files.
But there is this one that sometimes does not work, but most of the time it does.

The error I get is

Warning: IronPythonEvaluator.EvaluateIronPythonScript operation failed.
Traceback (most recent call last):
File “”, line 41, in
Exception: Error code: 5

I could not find what that error means, but usually, if I restart my PC it goes away.
I don’t know what is causing it either.

My python script is:

import clr
clr.AddReference('RevitNodes')
import Revit
clr.ImportExtensions(Revit.GeometryConversion)
from Revit.Elements import *
clr.AddReference('System')
from System.Collections.Generic import *
clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *
clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

doc = DocumentManager.Instance.CurrentDBDocument
uiapp = DocumentManager.Instance.CurrentUIApplication
app = uiapp.Application
#The inputs to this node will be stored as a list in the IN variables.
dataEnteringNode = IN
symbol = UnwrapElement(IN[0])
refPlane = UnwrapElement(IN[1][0])
location = UnwrapElement(IN[2])
referenceDirection = UnwrapElement(IN[3])
out = []

if type(refPlane) == "List":
	refPlane = refPlane[0]
	
TransactionManager.Instance.EnsureInTransaction(doc)

if symbol.IsActive == False:
	symbol.Activate()

doc.Regenerate()	

reference = Reference(refPlane)

for z in location:
	r1=[]
	for e in z:
		new = doc.Create.NewFamilyInstance(reference,e.ToXyz(),referenceDirection.ToXyz(),symbol)
		r1.append(new)
	out.append(r1)
TransactionManager.Instance.TransactionTaskDone()

#Assign your output to the OUT variable.
OUT = out

Help would be much appreciated.