Moved var statements to scope tops to avoid joisting confusion.