Move helper functions and polyfills outside of main function