Wrap entire code into anonymous function to protect global variables