Also, if you were to make a simple function that did this itself vexorians (if memory serves) map optimizer will inline it. here is a version of what you were doing with no BJ's in it, just replace that call with UnitItemName.
function UnitItemName takes unit u, integer itemId returns string...
After each wait AND at the very beginning of the trigger put this in:
if then else multiple actions
if (condition is true)
Boolean Comparison (SomeVariable is equal to True)
then (do actions)
Set SomeVariable = False (this is the Set Variable action)
Custom Script: return
else (do else...