moved function vars to top of function.