added env detection and wrap script in a single IIFE