Fix variable redeclaration, move declaration closer to use