Moved var declarations to the top of functions