Refactored my first function. Eliminated a global variable, eliminated two local variables inside the function, and shortened required code.