This explanation @awilliams has provided should hopefully explain what’s going on with the error.
I’m back at the PC tmw morning, so can test/post revisions to the code then (unless you have sorted it by then )
EDIT: @Paul_Wintour give this shot.