Right you are Richard, I will wrap the whole thing with 

     if (context.querySecManager())
     {
         . . . 
      }