moved some variable declarations to the top of the functiond definition