diff --git a/compiler2/ttcn3/Attributes.cc b/compiler2/ttcn3/Attributes.cc
index 5d6c389a544822c97e50dcde056e4c8689aa27a1..49091320f1fd7d0ec3ac99c39895f3b58976dbed 100644
--- a/compiler2/ttcn3/Attributes.cc
+++ b/compiler2/ttcn3/Attributes.cc
@@ -706,9 +706,9 @@ namespace Ttcn {
           act_field_err.qualifier->get_stringRepr().c_str(), omit_after_qual->get_stringRepr().c_str());
         continue;
       }
-      ErroneousAttributeSpec::indicator_t act_indicator = act_field_err.err_attr->get_indicator();
-      bool is_omit = act_field_err.err_attr->get_is_omit();
       if (act_field_err.subrefs_array.size()==1) { // erroneous value
+        ErroneousAttributeSpec::indicator_t act_indicator = act_field_err.err_attr->get_indicator();
+        bool is_omit = act_field_err.err_attr->get_is_omit();
         if (act_field_err.type_array.size()!=1) FATAL_ERROR("ErroneousAttributes::build_descr_tree()");
         if ((act_field_err.type_array[0]->get_typetype()==Type::T_SET_A) &&
             is_omit && (act_indicator!=ErroneousAttributeSpec::I_VALUE)) {
diff --git a/compiler2/ttcn3/TtcnTemplate.cc b/compiler2/ttcn3/TtcnTemplate.cc
index 95cec14501c4fd98b1f0508c308caa58256b3732..dfa43924c92aaa538e1b8fb1df77f27728e462fd 100644
--- a/compiler2/ttcn3/TtcnTemplate.cc
+++ b/compiler2/ttcn3/TtcnTemplate.cc
@@ -3129,9 +3129,9 @@ end:
     case NAMED_TEMPLATE_LIST:
       for (size_t i = 0;i < u.named_templates->get_nof_nts(); i++) {
         NamedTemplate *nt = u.named_templates->get_nt_byIndex(i);
-        Template* tmpl = nt->get_template();
         const string& name = nt->get_name().get_name();
         if (!checked_map.has_key(name)) {
+          Template* tmpl = nt->get_template();
           bool nrc = tmpl->chk_restriction(definition_name, TR_OMIT, usage_loc);
           needs_runtime_check = needs_runtime_check || nrc;
           checked_map.add(name, 0);
diff --git a/compiler2/xpather.cc b/compiler2/xpather.cc
index 63d60c1add5cc450621b6f267fa03f27bcf6b3ad..6a31d7be105763aade03cfe89bd42c47ccd7899f 100644
--- a/compiler2/xpather.cc
+++ b/compiler2/xpather.cc
@@ -515,11 +515,11 @@ void append_to_library_list (const char* prjName,
     actcfg);
   XPathObject exeObj(run_xpath(xpathCtx, exeXpath));
   Free(exeXpath);
-  std::string lib_name;
   if (exeObj->nodesetval && exeObj->nodesetval->nodeNr > 0) {
     const char* target_executable = (const char*)exeObj->nodesetval->nodeTab[0]->content;
     autostring target_exe_dir(get_dir_from_path(target_executable));
     autostring target_exe_file(get_file_from_path(target_executable));
+    std::string lib_name;
     lib_name = target_exe_file;
     ProjectDescriptor* projDesc = projGenHelper.getTargetOfProject(prjName);
     if (projDesc) {
@@ -1588,8 +1588,6 @@ static tpd_result process_tpd_internal(const char **p_tpd_name, char *tpdName, c
   map<cstring, const char> path_vars;
   
   autostring workdir;
-  
-  autostring proj_abs_workdir;
 
   autostring abs_workdir;
 
@@ -1798,6 +1796,8 @@ static tpd_result process_tpd_internal(const char **p_tpd_name, char *tpdName, c
   }
   const char *real_workdir = folders[workdir]; // This is relative to the location of the tpd file
   excluded_folders.add(real_workdir); // excluded by convention
+  
+  autostring proj_abs_workdir;
 
   // If -D flag was specified then we ignore the workdir
   // in the TPD (the current dir is considered the work dir).
@@ -2193,7 +2193,6 @@ static tpd_result process_tpd_internal(const char **p_tpd_name, char *tpdName, c
     xmlNodeSetPtr nodes = preincludeObj->nodesetval;
 
     if (nodes) for (int i = 0; i < nodes->nodeNr; ++i) {
-      char* content = (char*)preincludeObj->nodesetval->nodeTab[i]->content;
 
       // add includes to the end of list
       if (ttcn3_prep_includes) {
@@ -2206,6 +2205,7 @@ static tpd_result process_tpd_internal(const char **p_tpd_name, char *tpdName, c
           last_elem = last_elem->next;
           last_elem->next = NULL;
         }
+        char* content = (char*)preincludeObj->nodesetval->nodeTab[i]->content;
         replacechar(&content);
         last_elem->str = content;
       }
@@ -2223,7 +2223,6 @@ static tpd_result process_tpd_internal(const char **p_tpd_name, char *tpdName, c
     xmlNodeSetPtr nodes = ttcn3predefinesObj->nodesetval;
 
     if (nodes) for (int i = 0; i < nodes->nodeNr; ++i) {
-      const char* content = (const char*)ttcn3predefinesObj->nodesetval->nodeTab[i]->content;
 
       // add includes to the end of list
       if (ttcn3_prep_defines) {
@@ -2236,6 +2235,7 @@ static tpd_result process_tpd_internal(const char **p_tpd_name, char *tpdName, c
           last_elem = last_elem->next;
           last_elem->next = NULL;
         }
+        const char* content = (const char*)ttcn3predefinesObj->nodesetval->nodeTab[i]->content;
         last_elem->str = mcopystr(content);
       }
     }
@@ -2252,7 +2252,6 @@ static tpd_result process_tpd_internal(const char **p_tpd_name, char *tpdName, c
     xmlNodeSetPtr nodes = ttcn3preUndefinesObj->nodesetval;
 
     if (nodes) for (int i = 0; i < nodes->nodeNr; ++i) {
-      const char* content = (const char*)ttcn3preUndefinesObj->nodesetval->nodeTab[i]->content;
 
       // add includes to the end of list
       if (ttcn3_prep_undefines) {
@@ -2265,6 +2264,7 @@ static tpd_result process_tpd_internal(const char **p_tpd_name, char *tpdName, c
           last_elem = last_elem->next;
           last_elem->next = NULL;
         }
+        const char* content = (const char*)ttcn3preUndefinesObj->nodesetval->nodeTab[i]->content;
         last_elem->str = mcopystr(content);
       }
     }
@@ -2282,7 +2282,6 @@ static tpd_result process_tpd_internal(const char **p_tpd_name, char *tpdName, c
     xmlNodeSetPtr nodes = preincludesObj->nodesetval;
 
     if (nodes) for (int i = 0; i < nodes->nodeNr; ++i) {
-      char* content = (char*)preincludesObj->nodesetval->nodeTab[i]->content;
 
       // add includes to the end of list
       if (prep_includes) {
@@ -2295,6 +2294,7 @@ static tpd_result process_tpd_internal(const char **p_tpd_name, char *tpdName, c
           last_elem = last_elem->next;
           last_elem->next = NULL;
         }
+        char* content = (char*)preincludesObj->nodesetval->nodeTab[i]->content;
         replacechar(&content);
         last_elem->str = content;
       }
@@ -2312,7 +2312,6 @@ static tpd_result process_tpd_internal(const char **p_tpd_name, char *tpdName, c
     xmlNodeSetPtr nodes = predefinesObj->nodesetval;
 
     if (nodes) for (int i = 0; i < nodes->nodeNr; ++i) {
-      const char* content = (const char*)predefinesObj->nodesetval->nodeTab[i]->content;
 
       // add includes to the end of list
       if (prep_defines) {
@@ -2325,6 +2324,7 @@ static tpd_result process_tpd_internal(const char **p_tpd_name, char *tpdName, c
           last_elem = last_elem->next;
           last_elem->next = NULL;
         }
+        const char* content = (const char*)predefinesObj->nodesetval->nodeTab[i]->content;
         last_elem->str = mcopystr(content);
       }
     }
@@ -2341,7 +2341,6 @@ static tpd_result process_tpd_internal(const char **p_tpd_name, char *tpdName, c
     xmlNodeSetPtr nodes = preUndefinesObj->nodesetval;
 
     if (nodes) for (int i = 0; i < nodes->nodeNr; ++i) {
-      const char* content = (const char*)preUndefinesObj->nodesetval->nodeTab[i]->content;
 
       // add includes to the end of list
       if (prep_undefines) {
@@ -2354,6 +2353,7 @@ static tpd_result process_tpd_internal(const char **p_tpd_name, char *tpdName, c
           last_elem = last_elem->next;
           last_elem->next = NULL;
         }
+        const char* content = (const char*)preUndefinesObj->nodesetval->nodeTab[i]->content;
         last_elem->str = mcopystr(content);
       }
     }
@@ -2406,12 +2406,11 @@ static tpd_result process_tpd_internal(const char **p_tpd_name, char *tpdName, c
     xmlNodeSetPtr nodes = solspeclibObj->nodesetval;
 
     if (nodes) for (int i = 0; i < nodes->nodeNr; ++i) {
-      char* content = (char*)solspeclibObj->nodesetval->nodeTab[i]->content;
 
       // add includes to the end of list
       if (solspeclibs) {
         // go to last element
-        struct string_list* last_elem =solspeclibs;
+        struct string_list* last_elem = solspeclibs;
         while (last_elem->next) last_elem = last_elem->next;
         // add string to last element if empty or create new last element and add it to that
         if (last_elem->str) {
@@ -2419,6 +2418,7 @@ static tpd_result process_tpd_internal(const char **p_tpd_name, char *tpdName, c
           last_elem = last_elem->next;
           last_elem->next = NULL;
         }
+        char* content = (char*)solspeclibObj->nodesetval->nodeTab[i]->content;
         replacechar(&content);
         last_elem->str = content;
       }
@@ -2436,7 +2436,6 @@ static tpd_result process_tpd_internal(const char **p_tpd_name, char *tpdName, c
     xmlNodeSetPtr nodes = sol8speclibObj->nodesetval;
 
     if (nodes) for (int i = 0; i < nodes->nodeNr; ++i) {
-      char* content = (char*)sol8speclibObj->nodesetval->nodeTab[i]->content;
 
       // add includes to the end of list
       if (sol8speclibs) {
@@ -2449,6 +2448,7 @@ static tpd_result process_tpd_internal(const char **p_tpd_name, char *tpdName, c
           last_elem = last_elem->next;
           last_elem->next = NULL;
         }
+        char* content = (char*)sol8speclibObj->nodesetval->nodeTab[i]->content;
         replacechar(&content);
         last_elem->str = content;
       }
@@ -2466,7 +2466,6 @@ static tpd_result process_tpd_internal(const char **p_tpd_name, char *tpdName, c
     xmlNodeSetPtr nodes = linuxspeclibObj->nodesetval;
 
     if (nodes) for (int i = 0; i < nodes->nodeNr; ++i) {
-      char* content = (char*)linuxspeclibObj->nodesetval->nodeTab[i]->content;
 
       // add includes to the end of list
       if (linuxspeclibs) {
@@ -2479,6 +2478,7 @@ static tpd_result process_tpd_internal(const char **p_tpd_name, char *tpdName, c
           last_elem = last_elem->next;
           last_elem->next = NULL;
         }
+        char* content = (char*)linuxspeclibObj->nodesetval->nodeTab[i]->content;
         replacechar(&content);
         last_elem->str = content;
       }
@@ -2496,7 +2496,6 @@ static tpd_result process_tpd_internal(const char **p_tpd_name, char *tpdName, c
     xmlNodeSetPtr nodes = freebsdspeclibObj->nodesetval;
 
     if (nodes) for (int i = 0; i < nodes->nodeNr; ++i) {
-      char* content = (char*)freebsdspeclibObj->nodesetval->nodeTab[i]->content;
 
       // add includes to the end of list
       if (freebsdspeclibs) {
@@ -2509,6 +2508,7 @@ static tpd_result process_tpd_internal(const char **p_tpd_name, char *tpdName, c
           last_elem = last_elem->next;
           last_elem->next = NULL;
         }
+        char* content = (char*)freebsdspeclibObj->nodesetval->nodeTab[i]->content;
         replacechar(&content);
         last_elem->str = content;
       }
@@ -2526,7 +2526,6 @@ static tpd_result process_tpd_internal(const char **p_tpd_name, char *tpdName, c
     xmlNodeSetPtr nodes = win32speclibObj->nodesetval;
 
     if (nodes) for (int i = 0; i < nodes->nodeNr; ++i) {
-      char* content = (char*)win32speclibObj->nodesetval->nodeTab[i]->content;
 
       // add includes to the end of list
       if (win32speclibs) {
@@ -2539,6 +2538,7 @@ static tpd_result process_tpd_internal(const char **p_tpd_name, char *tpdName, c
           last_elem = last_elem->next;
           last_elem->next = NULL;
         }
+        char* content = (char*)win32speclibObj->nodesetval->nodeTab[i]->content;
         replacechar(&content);
         last_elem->str = content;
       }
@@ -2570,7 +2570,6 @@ static tpd_result process_tpd_internal(const char **p_tpd_name, char *tpdName, c
     xmlNodeSetPtr nodes = additionalObjectsObj->nodesetval;
 
     if (nodes) for (int i = 0; i < nodes->nodeNr; ++i) {
-      char* content = (char*)additionalObjectsObj->nodesetval->nodeTab[i]->content;
 
       // add to the end of list
       if (additionalObjects) {
@@ -2583,6 +2582,7 @@ static tpd_result process_tpd_internal(const char **p_tpd_name, char *tpdName, c
           last_elem = last_elem->next;
           last_elem->next = NULL;
         }
+        char* content = (char*)additionalObjectsObj->nodesetval->nodeTab[i]->content;
         replacechar(&content);
         last_elem->str = content;
       }
@@ -2610,7 +2610,6 @@ static tpd_result process_tpd_internal(const char **p_tpd_name, char *tpdName, c
     xmlNodeSetPtr nodes = linkerlibsObj->nodesetval;
 
     if (nodes) for (int i = 0; i < nodes->nodeNr; ++i) {
-      char* content = (char*)linkerlibsObj->nodesetval->nodeTab[i]->content;
 
       // add includes to the end of list
       if (linkerlibs) {
@@ -2623,6 +2622,7 @@ static tpd_result process_tpd_internal(const char **p_tpd_name, char *tpdName, c
           last_elem = last_elem->next;
           last_elem->next = NULL;
         }
+        char* content = (char*)linkerlibsObj->nodesetval->nodeTab[i]->content;
         replacechar(&content);
         last_elem->str = content;
 
@@ -2643,7 +2643,6 @@ static tpd_result process_tpd_internal(const char **p_tpd_name, char *tpdName, c
     xmlNodeSetPtr nodes = linkerlibsearchObj->nodesetval;
 
     if (nodes) for (int i = 0; i < nodes->nodeNr; ++i) {
-      char* content = (char*)linkerlibsearchObj->nodesetval->nodeTab[i]->content;
 
       // add includes to the end of list
       if (linkerlibsearchp) {
@@ -2656,6 +2655,7 @@ static tpd_result process_tpd_internal(const char **p_tpd_name, char *tpdName, c
           last_elem = last_elem->next;
           last_elem->next = NULL;
         }
+        char* content = (char*)linkerlibsearchObj->nodesetval->nodeTab[i]->content;
         replacechar(&content);
         last_elem->str = content;
 
@@ -2672,8 +2672,8 @@ static tpd_result process_tpd_internal(const char **p_tpd_name, char *tpdName, c
       actcfg);
     XPathObject generatorCommandObj(run_xpath(xpathCtx, generatorCommandXpath));
     Free(generatorCommandXpath);
-    autostring generatorCommand;
     if (generatorCommandObj->nodesetval && generatorCommandObj->nodesetval->nodeNr > 0) {
+      autostring generatorCommand;
       generatorCommand = mcopystr((const char*)generatorCommandObj->nodesetval->nodeTab[0]->content);
       // run the command and capture the output
       printf("Executing generator command `%s' specified in `%s'...\n", (const char*)generatorCommand, (const char*)abs_tpd_name);
@@ -3212,11 +3212,11 @@ static tpd_result process_tpd_internal(const char **p_tpd_name, char *tpdName, c
   }
   // Print the TPD too.
   if (*p_Pflag) {
-    autostring dir_part(get_dir_from_path(*p_tpd_name));
-    autostring file_part(get_file_from_path(*p_tpd_name));
     if (*p_aflag) {
       puts((const char *)abs_tpd_name);
     } else {
+      autostring dir_part(get_dir_from_path(*p_tpd_name));
+      autostring file_part(get_file_from_path(*p_tpd_name));
       autostring rel_dir_part(get_relative_dir(dir_part, file_list_path ? file_list_path : abs_tpd_dir));
       autostring rel_dir_file_part(compose_path_name(rel_dir_part, file_part));
       const char *rel_tpd_name = (const char *)rel_dir_file_part;
diff --git a/core/Float.cc b/core/Float.cc
index 39b22439684142d233ce7c7de1734cc8833d3ffc..92aaa21e9c74051446731d4b7d2919efec18dc60 100644
--- a/core/Float.cc
+++ b/core/Float.cc
@@ -946,7 +946,7 @@ int FLOAT::XER_decode(const XERdescriptor_t& p_td, XmlReaderWrap& reader,
 {
   bound_flag = FALSE;
   boolean exer  = is_exer(flavor);
-  int success = reader.Ok(), depth = -1;
+  int success = reader.Ok();
   if (success <= 0) return 0;
   boolean own_tag = !(exer && (p_td.xer_bits & UNTAGGED)) && !is_exerlist(flavor);
 
@@ -989,6 +989,7 @@ tagless:
     // Let the caller do reader.AdvanceAttribute();
   }
   else {
+    int depth = -1;
     for (; success == 1; success = reader.Read()) {
       int type = reader.NodeType();
       if (XML_READER_TYPE_ELEMENT == type) {
diff --git a/core/Hexstring.cc b/core/Hexstring.cc
index db576551104d20bd48c7aed827d78f2c954c27e1..93f523816cc5ed4c91b8a6c247812362b865637a 100644
--- a/core/Hexstring.cc
+++ b/core/Hexstring.cc
@@ -427,13 +427,13 @@ HEXSTRING HEXSTRING::operator>>(int shift_count) const
     int n_nibbles = val_ptr->n_nibbles;
     if (n_nibbles == 0) return *this;
     HEXSTRING ret_val(n_nibbles);
-    int n_bytes = (n_nibbles + 1) / 2;
     clear_unused_nibble();
     if (shift_count > n_nibbles) shift_count = n_nibbles;
     int shift_bytes = shift_count / 2;
     memset(ret_val.val_ptr->nibbles_ptr, 0, shift_bytes);
     if (shift_count % 2) {
       ret_val.val_ptr->nibbles_ptr[shift_bytes] = val_ptr->nibbles_ptr[0] << 4;
+      int n_bytes = (n_nibbles + 1) / 2;
       int byte_count = shift_bytes + 1;
       for (; byte_count < n_bytes; byte_count++) {
         ret_val.val_ptr->nibbles_ptr[byte_count]
diff --git a/core/LegacyLogger.cc b/core/LegacyLogger.cc
index 5abd4be365a29cd380fbbdfac5672b5a75e56dcc..f11af83a5a781a30eea822340e03a38f1871eca4 100644
--- a/core/LegacyLogger.cc
+++ b/core/LegacyLogger.cc
@@ -177,11 +177,11 @@ void LegacyLogger::fatal_error(const char *err_msg, ...)
 void LegacyLogger::log(const TitanLoggerApi::TitanLogEvent& event,
     boolean log_buffered, boolean separate_file, boolean use_emergency_mask)
 {
-  const TTCN_Logger::Severity& severity = (const TTCN_Logger::Severity)(int)event.severity();
 
   if (separate_file) {
     log_file_emerg(event);
   } else {
+    const TTCN_Logger::Severity& severity = (const TTCN_Logger::Severity)(int)event.severity();
     if (use_emergency_mask) {
       if (TTCN_Logger::should_log_to_emergency(severity)
           ||
@@ -262,10 +262,10 @@ boolean LegacyLogger::log_file(const TitanLoggerApi::TitanLogEvent& event,
 {
   if (!this->log_fp_) return FALSE;
 
-  struct timeval event_timestamp = { (time_t)event.timestamp().seconds(),
-    (suseconds_t)event.timestamp().microSeconds() };
   if (this->is_disk_full_) {
     if (this->disk_full_action_.type == TTCN_Logger::DISKFULL_RETRY) {
+      struct timeval event_timestamp = { (time_t)event.timestamp().seconds(),
+        (suseconds_t)event.timestamp().microSeconds() };
       struct timeval diff;
       // If the specified time period has elapsed retry logging to file.
       if (event_timestamp.tv_usec < this->disk_full_time_.tv_usec) {
@@ -1801,10 +1801,10 @@ char *event_to_str(const TitanLoggerApi::TitanLogEvent& event,
           if (sourceinfo) sourceinfo = mputstr(sourceinfo, "->");
           const char *file_name = loc.filename();
           int line_number = loc.line();
-          const char *entity_name = loc.ent__name();
-          API::LocationInfo_ent__type entity_type = loc.ent__type();
           sourceinfo = mputprintf(sourceinfo, "%s:%u", file_name, line_number);
           if (TTCN_Logger::get_log_entity_name()) {
+            API::LocationInfo_ent__type entity_type = loc.ent__type();
+            const char *entity_name = loc.ent__name();
             switch (entity_type) {
             case API::LocationInfo_ent__type::controlpart:
               sourceinfo = mputprintf(sourceinfo, "(controlpart:%s)", entity_name);
diff --git a/core/Template.cc b/core/Template.cc
index c761cb3aa93056f1f21b54de20c93152a86754dc..b515887cdc97a0c103236b9ab792e63f7de16741 100644
--- a/core/Template.cc
+++ b/core/Template.cc
@@ -2288,12 +2288,12 @@ void Set_Of_Template::log_matchv(const Base_Type* match_value, boolean legacy) c
       TTCN_Logger::print_logmatch_buffer();
       TTCN_Logger::log_event_str(" matched");
     } else {
-      size_t previous_size = TTCN_Logger::get_logmatch_buffer_len();
       if (template_selection == SPECIFIC_VALUE) {
         const Record_Of_Type* setof_value = static_cast<const Record_Of_Type*>(match_value);
         log_match_heuristics(setof_value, setof_value->size_of(), this,
           single_value.n_elements, match_function_specific, log_function, legacy);
       } else {
+        size_t previous_size = TTCN_Logger::get_logmatch_buffer_len();
         if (previous_size != 0) {
           TTCN_Logger::print_logmatch_buffer();
           TTCN_Logger::set_logmatch_buffer_len(previous_size);
diff --git a/core/XER.cc b/core/XER.cc
index 81c8d1cd4b536e078cbd81ca0431aff2ad8a6ea8..ca4934b9c04fc131bca53151c322cefe9c1e8672 100644
--- a/core/XER.cc
+++ b/core/XER.cc
@@ -52,7 +52,6 @@ int do_indent(TTCN_Buffer& buf, int level)
 const char* verify_name(XmlReaderWrap& reader, const XERdescriptor_t& p_td, boolean exer)
 {
   const char *name = (const char*)reader.LocalName();
-  const char *nsuri= (const char*)reader.NamespaceUri(); // NULL if no ns
 
   const namespace_t *expected_ns = 0;
   if (p_td.my_module != 0 && p_td.ns_index != -1) {
@@ -72,6 +71,7 @@ const char* verify_name(XmlReaderWrap& reader, const XERdescriptor_t& p_td, bool
 
   if (exer) { // XML namespaces only apply to EXER
     const char *prefix = (const char*)reader.Prefix(); // may be NULL
+    const char *nsuri = (const char*)reader.NamespaceUri(); // NULL if no ns
     if (expected_ns == 0) { // get_ns was not called
       if (nsuri != 0) {
         TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_TAG,
diff --git a/core2/Basetype2.cc b/core2/Basetype2.cc
index 2c0b7a7eb0139ad449e532c236badd678edadbe8..6680a650e2b953eb03a9554c3720541b68434af4 100644
--- a/core2/Basetype2.cc
+++ b/core2/Basetype2.cc
@@ -1256,7 +1256,6 @@ int Record_Of_Type::RAW_decode(const TTCN_Typedescriptor_t& p_td,
   limit -= prepaddlength;
   int decoded_length = 0;
   int decoded_field_length = 0;
-  size_t start_of_field = 0;
   if (first_call) {
     set_size(0);
   }
@@ -1279,6 +1278,7 @@ int Record_Of_Type::RAW_decode(const TTCN_Typedescriptor_t& p_td,
       if (!first_call) return -1;
       goto finished;
     }
+    size_t start_of_field = 0;
     while (limit > 0) {
       start_of_field = buff.get_pos_bit();
       Base_Type* field_bt = get_at(a); // non-const, extend the record-of
@@ -1587,10 +1587,9 @@ int Record_Of_Type::JSON_decode(const TTCN_Typedescriptor_t& p_td, JSON_Tokenize
   for (int nof_elements = 0; TRUE; ++nof_elements) {
     // Read value tokens until we reach some other token
     size_t buf_pos = p_tok.get_buf_pos();
-    size_t ret_val;
     if (NULL != p_td.json && p_td.json->metainfo_unbound) {
       // check for metainfo object
-      ret_val = p_tok.get_next_token(&token, NULL, NULL);
+      size_t ret_val = p_tok.get_next_token(&token, NULL, NULL);
       if (JSON_TOKEN_OBJECT_START == token) {
         char* value = NULL;
         size_t value_len = 0;
@@ -3413,7 +3412,6 @@ int Record_Type::RAW_decode(const TTCN_Typedescriptor_t& p_td, TTCN_Buffer& buff
 {
   int field_cnt = get_count();
   int opt_cnt = optional_count();
-  int mand_num = field_cnt - opt_cnt; // expected mandatory fields
 
   raw_order_t local_top_order;
   if (p_td.raw->top_bit_order == TOP_BIT_INHERITED) local_top_order = top_bit_ord;
@@ -3463,6 +3461,7 @@ int Record_Type::RAW_decode(const TTCN_Typedescriptor_t& p_td, TTCN_Buffer& buff
 continue_while: ;
     }
     delete[] field_map;
+    int mand_num = field_cnt - opt_cnt; // expected mandatory fields
     if (mand_num > 0 && nof_mand_fields != mand_num) {
       /* Not all required fields were decoded. If there are no bits left,
        * that means that the last field was decoded successfully but used up
@@ -4062,10 +4061,6 @@ const XERdescriptor_t* Record_Type::xer_descr(int /*field_index*/) const
 char ** Record_Type::collect_ns(const XERdescriptor_t& p_td, size_t& num, bool& def_ns, unsigned int flavor) const
 {
   const int field_cnt = get_count();
-  // The USE-ORDER member is first, unless preempted by EMBED-VALUES
-  const int uo_index = ((p_td.xer_bits & EMBED_VALUES) !=0);
-  // Index of the first "normal" member (after E-V and U-O)
-  const int start_at = uo_index + ((p_td.xer_bits & USE_ORDER) != 0);
 
   size_t num_collected = 0;
   // First, our own namespace. Sets num_collected to 0 or 1.
@@ -4083,7 +4078,12 @@ char ** Record_Type::collect_ns(const XERdescriptor_t& p_td, size_t& num, bool&
 
       collected_ns[num_collected-1] = mprintf(" xmlns:%s='%s'", c_ns->px, c_ns->ns);
     }
-
+  
+    // The USE-ORDER member is first, unless preempted by EMBED-VALUES
+    const int uo_index = ((p_td.xer_bits & EMBED_VALUES) !=0);
+    // Index of the first "normal" member (after E-V and U-O)
+    const int start_at = uo_index + ((p_td.xer_bits & USE_ORDER) != 0);
+    
     // Collect namespace declarations from all components (recursively).
     // This is extremely nasty, but we can't prosecute you for that.
     // (Monty Python - Crunchy Frog sketch). This whole thing is O(n^3). Yuck.
@@ -4141,9 +4141,6 @@ int Record_Type::XER_encode(const XERdescriptor_t& p_td, TTCN_Buffer& p_buf,
   const int num_attributes = get_xer_num_attr();
   // The USE-ORDER member is first, unless preempted by EMBED-VALUES
   const int uo_index = ((p_td.xer_bits & EMBED_VALUES) !=0);
-  // Index of the first "normal" member (after E-V and U-O)
-  const int start_at = uo_index + ((p_td.xer_bits & USE_ORDER) != 0);
-  const int first_nonattr = start_at + num_attributes;
   // start_tag_len is keeping track of how much was written at the end of the
   // start tag, i.e. the ">\n". This is used later to "back up" over it.
   int start_tag_len = 1 + indenting;
@@ -4282,7 +4279,11 @@ int Record_Type::XER_encode(const XERdescriptor_t& p_td, TTCN_Buffer& p_buf,
       flavor &= ~DEF_NS_PRESENT;
       flavor |=  DEF_NS_SQUASHED;
     }
-
+    
+    // Index of the first "normal" member (after E-V and U-O)
+    const int start_at = uo_index + ((p_td.xer_bits & USE_ORDER) != 0);
+    const int first_nonattr = start_at + num_attributes;
+    
     /* First all the attributes (not added to sub_len) */
     int i;
     for (i = start_at; i < first_nonattr; ++i) {