Moved var declarations where they are used