Some dodgy code in there, global 'result' var, fixed.  Also, pass values...

... to the extend, seems like that is required otherwise values don't seem available