This will be a problem for sure I think, because we are re-using the same scripting context for each patient.  So, by only setting this in the script context if it is non-null, you will end up picking up the previous patient's value accidentally.  We should just put the result into the context whether it is null or not.