moved var to the top
added missing var declaration
moved var to the top