moving var declaration to top of function