Fix scope of some variables

as reported by Cppcheck