Fixed scope issue.

Fixed scope issue to prevent "undefined function" errors when some other lib redefines the global `device` variable.