Fixed cppcheck errors: reduce variables scope