diff --git a/regression_test/XML/XmlWorkflow/XmlTest_expectedTtcns/www_example_org_pref1_e.ttcn b/regression_test/XML/XmlWorkflow/XmlTest_expectedTtcns/www_example_org_pref1_e.ttcn
new file mode 100644
index 0000000000000000000000000000000000000000..808ba6a548242d542d5708ef15f722629b5177e2
--- /dev/null
+++ b/regression_test/XML/XmlWorkflow/XmlTest_expectedTtcns/www_example_org_pref1_e.ttcn
@@ -0,0 +1,95 @@
+/*******************************************************************************
+* Copyright (c) 2000-2016 Ericsson Telecom AB
+*
+* XSD to TTCN-3 Translator version: CRL 113 200/5 R5B01                     
+*
+* All rights reserved. This program and the accompanying materials
+* are made available under the terms of the Eclipse Public License v1.0
+* which accompanies this distribution, and is available at
+* http://www.eclipse.org/legal/epl-v10.html
+
+* Contributors:
+*   Szabo, Bence Janos
+*******************************************************************************/
+//
+//  File:          www_example_org_pref1_e.ttcn
+//  Description:
+//  References:
+//  Rev:
+//  Prodnr:
+//  Updated:       Thu Sep 29 08:47:37 2015
+//  Contact:       http://ttcn.ericsson.se
+//
+////////////////////////////////////////////////////////////////////////////////
+//	Generated from file(s):
+//	- www_example_org_perf1_e.xsd
+//			/* xml version = "1.0" encoding = "UTF-8" */
+//			/* targetnamespace = "www.example.org/pref1/e" */
+////////////////////////////////////////////////////////////////////////////////
+//     Modification header(s):
+//-----------------------------------------------------------------------------
+//  Modified by:
+//  Modification date:
+//  Description:
+//  Modification contact:
+//------------------------------------------------------------------------------
+////////////////////////////////////////////////////////////////////////////////
+
+
+module www_example_org_pref1_e {
+
+
+import from XSD all;
+
+
+import from www_example_org_pref2 all;
+
+
+import from www_example_org_pref3 all;
+
+
+type record MyElement_2
+{
+	www_example_org_pref2.MyElement myElement optional,
+	www_example_org_pref2.MyElement2 myElement2 optional,
+	XSD.Integer myField,
+	www_example_org_pref2.MyType myField2,
+	www_example_org_pref2.MyElement myElement_1,
+	www_example_org_pref2.MyElement2 myElement2_1
+}
+with {
+  variant "name as 'MyElement'";
+  variant "element";
+  variant (myElement) "name as capitalized";
+  variant (myElement) "namespace as 'www.example.org/pref2' prefix 'ns2'";
+  variant (myElement) "attribute";
+  variant (myElement2) "name as capitalized";
+  variant (myElement2) "namespace as 'www.example.org/pref2' prefix 'ns2'";
+  variant (myElement2) "attribute";
+  variant (myElement_1) "name as 'MyElement'";
+  variant (myElement_1) "namespace as 'www.example.org/pref2' prefix 'ns2'";
+  variant (myElement2_1) "name as 'MyElement2'";
+  variant (myElement2_1) "namespace as 'www.example.org/pref2' prefix 'ns2'";
+};
+
+
+type www_example_org_pref2.MyElement MyElement
+with {
+  variant "element";
+};
+
+
+type www_example_org_pref2.MyElement2 MyElement_1
+with {
+  variant "name as 'MyElement'";
+  variant "element";
+};
+
+
+}
+with {
+  encode "XML";
+  variant "namespace as 'www.example.org/pref1/e' prefix 'ns'";
+  variant "controlNamespace 'http://www.w3.org/2001/XMLSchema-instance' prefix 'xsi'";
+  variant "elementFormQualified";
+}
diff --git a/regression_test/XML/XmlWorkflow/XmlTest_expectedTtcns/www_example_org_pref2_e.ttcn b/regression_test/XML/XmlWorkflow/XmlTest_expectedTtcns/www_example_org_pref2_e.ttcn
new file mode 100644
index 0000000000000000000000000000000000000000..8681260da827aa6f0d6bf8d1d7702b7bc2631160
--- /dev/null
+++ b/regression_test/XML/XmlWorkflow/XmlTest_expectedTtcns/www_example_org_pref2_e.ttcn
@@ -0,0 +1,68 @@
+/*******************************************************************************
+* Copyright (c) 2000-2016 Ericsson Telecom AB
+*
+* XSD to TTCN-3 Translator version: CRL 113 200/5 R5B01                     
+*
+* All rights reserved. This program and the accompanying materials
+* are made available under the terms of the Eclipse Public License v1.0
+* which accompanies this distribution, and is available at
+* http://www.eclipse.org/legal/epl-v10.html
+
+* Contributors:
+*   Szabo, Bence Janos
+*******************************************************************************/
+//
+//  File:          www_example_org_pref2_e.ttcn
+//  Description:
+//  References:
+//  Rev:
+//  Prodnr:
+//  Updated:       Thu Sep 29 08:47:37 2015
+//  Contact:       http://ttcn.ericsson.se
+//
+////////////////////////////////////////////////////////////////////////////////
+//	Generated from file(s):
+//	- www_example_org_pref2_e.xsd
+//			/* xml version = "1.0" encoding = "UTF-8" */
+//			/* targetnamespace = "www.example.org/pref2/e" */
+////////////////////////////////////////////////////////////////////////////////
+//     Modification header(s):
+//-----------------------------------------------------------------------------
+//  Modified by:
+//  Modification date:
+//  Description:
+//  Modification contact:
+//------------------------------------------------------------------------------
+////////////////////////////////////////////////////////////////////////////////
+
+
+module www_example_org_pref2 {
+
+
+import from XSD all;
+
+
+type MyType MyElement
+with {
+  variant "element";
+};
+
+
+type MyType MyElement2
+with {
+  variant "element";
+};
+
+
+type record MyType
+{
+	XSD.Integer myField
+};
+
+
+}
+with {
+  encode "XML";
+  variant "namespace as 'www.example.org/pref2/e' prefix 'ns'";
+  variant "controlNamespace 'http://www.w3.org/2001/XMLSchema-instance' prefix 'xsi'";
+}
diff --git a/regression_test/XML/XmlWorkflow/XmlTest_expectedTtcns/www_example_org_pref3_e.ttcn b/regression_test/XML/XmlWorkflow/XmlTest_expectedTtcns/www_example_org_pref3_e.ttcn
new file mode 100644
index 0000000000000000000000000000000000000000..8deff0c691e4a1f74b5c5a60a356dc8b2d82048e
--- /dev/null
+++ b/regression_test/XML/XmlWorkflow/XmlTest_expectedTtcns/www_example_org_pref3_e.ttcn
@@ -0,0 +1,62 @@
+/*******************************************************************************
+* Copyright (c) 2000-2016 Ericsson Telecom AB
+*
+* XSD to TTCN-3 Translator version: CRL 113 200/5 R5B01                     
+*
+* All rights reserved. This program and the accompanying materials
+* are made available under the terms of the Eclipse Public License v1.0
+* which accompanies this distribution, and is available at
+* http://www.eclipse.org/legal/epl-v10.html
+
+* Contributors:
+*   Szabo, Bence Janos
+*******************************************************************************/
+//
+//  File:          www_example_org_pref3_e.ttcn
+//  Description:
+//  References:
+//  Rev:
+//  Prodnr:
+//  Updated:       Thu Sep 29 08:47:37 2015
+//  Contact:       http://ttcn.ericsson.se
+//
+////////////////////////////////////////////////////////////////////////////////
+//	Generated from file(s):
+//	- www_example_org_pref3_e.xsd
+//			/* xml version = "1.0" encoding = "UTF-8" */
+//			/* targetnamespace = "www.example.org/pref3/e" */
+////////////////////////////////////////////////////////////////////////////////
+//     Modification header(s):
+//-----------------------------------------------------------------------------
+//  Modified by:
+//  Modification date:
+//  Description:
+//  Modification contact:
+//------------------------------------------------------------------------------
+////////////////////////////////////////////////////////////////////////////////
+
+
+module www_example_org_pref3 {
+
+
+import from XSD all;
+
+
+type MyType MyElement2
+with {
+  variant "element";
+};
+
+
+type record MyType
+{
+	XSD.Integer myField
+};
+
+
+}
+with {
+  encode "XML";
+  variant "namespace as 'www.example.org/pref3/e' prefix 'ns'";
+  variant "controlNamespace 'http://www.w3.org/2001/XMLSchema-instance' prefix 'xsi'";
+}
diff --git a/regression_test/XML/XmlWorkflow/src/xmlTest.prj b/regression_test/XML/XmlWorkflow/src/xmlTest.prj
index 6c60f9bb1496c907aa1f07621704274a1aa7a505..df3e15608e4dc6aa8795339064de45c065ef1143 100644
--- a/regression_test/XML/XmlWorkflow/src/xmlTest.prj
+++ b/regression_test/XML/XmlWorkflow/src/xmlTest.prj
@@ -165,6 +165,9 @@
 		<File path="../xsd/list_complextype.xsd" />
 		<File path="../xsd/enumeration_restriction3.xsd" />
 		<File path="../xsd/MyXMLSchema.xsd" />
+        <File path="../xsd/www_example_org_pref1.xsd" />
+        <File path="../xsd/www_example_org_pref2.xsd" />
+        <File path="../xsd/www_example_org_pref3.xsd" />
             </File_Group>
             <File_Group name="XmlTest_xsds" >
                 <File path="../XmlTest_xsds/XmlTest_boolean.xsd" />
@@ -400,6 +403,9 @@
 	        <File path="../XmlTest_expectedTtcns/www_example_org_list_complextype_e.ttcn" />
 	        <File path="../XmlTest_expectedTtcns/www_example_org_enumeration_restriction3_e.ttcn" />
 	        <File path="../XmlTest_expectedTtcns/http_www_example_org_2001_XMLSchema_e.ttcn" />
+            <File path="../XmlTest_expectedTtcns/www_example_org_pref1_e.ttcn" />
+            <File path="../XmlTest_expectedTtcns/www_example_org_pref2_e.ttcn" />
+            <File path="../XmlTest_expectedTtcns/www_example_org_pref3_e.ttcn" />
             </File_Group>
             <File_Group name="XmlTest_src" >
                 <File path="xmlTest_Shell.ttcn" />
diff --git a/regression_test/XML/XmlWorkflow/src/xmlTest_Testcases.ttcn b/regression_test/XML/XmlWorkflow/src/xmlTest_Testcases.ttcn
index 681e3887724a2edb4d5acd8149eff18fbe5dfe90..f47e971cca76b19a2f28c1728863219c443ae5e1 100644
--- a/regression_test/XML/XmlWorkflow/src/xmlTest_Testcases.ttcn
+++ b/regression_test/XML/XmlWorkflow/src/xmlTest_Testcases.ttcn
@@ -1812,6 +1812,23 @@ group ComplexType {
     }
   }//tc_
 
+  testcase tc_ambiguous_type_namespace_prefix() runs on xmlTest_CT {
+    f_shellCommandWithVerdict(xsd2ttcn_command & " www_example_org_pref1.xsd www_example_org_pref2.xsd www_example_org_pref3.xsd","",c_shell_successWithoutWarningAndError);
+
+    if(getverdict==pass) {
+      f_compareFiles(
+        "www_example_org_pref1_e.ttcn","www_example_org_pref1.ttcn", c_numOfDiff_headerAndModuleName);
+    }
+    if(getverdict==pass) {
+      f_compareFiles(
+        "www_example_org_pref2_e.ttcn","www_example_org_pref2.ttcn", c_numOfDiff_headerAndModuleName);
+    }
+    if(getverdict==pass) {
+      f_compareFiles(
+        "www_example_org_pref3_e.ttcn","www_example_org_pref3.ttcn", c_numOfDiff_headerAndModuleName);
+    }
+  }//tc_
+
 
   //=========================================================================
   //  tc_complex_any_pos1_encDec
@@ -2683,6 +2700,7 @@ control {
   execute(tc_complex_import_nameCollision2_converter());
   execute(tc_complex_any_pos_converter());
   execute(tc_imported_type_prefix());
+  execute(tc_ambiguous_type_namespace_prefix());
   execute(tc_complex_any_pos1_encDec()); //Failed, TR: HL37887
   execute(tc_complex_any_pos2_encDec());
   execute(tc_complex_any_pos3_encDec());//failed, TR:
diff --git a/regression_test/XML/XmlWorkflow/xsd/www_example_org_pref1.xsd b/regression_test/XML/XmlWorkflow/xsd/www_example_org_pref1.xsd
new file mode 100644
index 0000000000000000000000000000000000000000..d55221cf6aa75ebbb9b86dc6e82d1f40d2673227
--- /dev/null
+++ b/regression_test/XML/XmlWorkflow/xsd/www_example_org_pref1.xsd
@@ -0,0 +1,27 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<schema xmlns="http://www.w3.org/2001/XMLSchema"
+    elementFormDefault="qualified"
+    targetNamespace="www.example.org/pref1"
+    xmlns:ns="www.example.org/pref1"
+    xmlns:ns2="www.example.org/pref2"
+    xmlns:ns3="www.example.org/pref3">
+  <import namespace="www_example_org_pref2" schemaLocation="www_example_org_pref2.xsd" />
+  <import namespace="www_example_org_pref3" schemaLocation="www_example_org_pref3.xsd" />
+  <element name="MyElement">
+	<complexType>
+		<sequence>
+			<element name="myField" type="integer" />
+			<element name="myField2" type="ns2:MyType"/>
+			<element ref="ns2:MyElement"/>
+			<element ref="ns2:MyElement2"/>
+		</sequence>
+		<attribute ref="ns2:MyElement"/>
+		<attribute ref="ns2:MyElement2"/>
+	</complexType>
+  </element>
+
+  <element name="MyElement" type="ns2:MyElement"/>
+
+  <element name="MyElement" type="ns2:MyElement2"/>
+
+</schema>
diff --git a/regression_test/XML/XmlWorkflow/xsd/www_example_org_pref2.xsd b/regression_test/XML/XmlWorkflow/xsd/www_example_org_pref2.xsd
new file mode 100644
index 0000000000000000000000000000000000000000..4502f259e555eb32deecb1ea28a712e25f61c41c
--- /dev/null
+++ b/regression_test/XML/XmlWorkflow/xsd/www_example_org_pref2.xsd
@@ -0,0 +1,13 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<schema xmlns="http://www.w3.org/2001/XMLSchema"
+	elementFormDefault="unqualified" 
+	targetNamespace="www.example.org/pref2"
+	xmlns:ns="www.example.org/pref2">
+	<element name="MyElement" type="ns:MyType" />
+	<element name="MyElement2" type="ns:MyType" />
+	<complexType name="MyType">
+		<sequence>
+			<element name="myField" type="integer" />
+		</sequence>
+	</complexType>
+</schema>
diff --git a/regression_test/XML/XmlWorkflow/xsd/www_example_org_pref3.xsd b/regression_test/XML/XmlWorkflow/xsd/www_example_org_pref3.xsd
new file mode 100644
index 0000000000000000000000000000000000000000..fefec8c141ce914a77f77fd0d60283ada08e3c0f
--- /dev/null
+++ b/regression_test/XML/XmlWorkflow/xsd/www_example_org_pref3.xsd
@@ -0,0 +1,12 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<schema xmlns="http://www.w3.org/2001/XMLSchema"
+	elementFormDefault="unqualified" 
+	targetNamespace="www.example.org/pref3"
+	xmlns:ns="www.example.org/pref3">
+	<element name="MyElement2" type="ns:MyType" />
+	<complexType name="MyType">
+		<sequence>
+			<element name="myField" type="integer" />
+		</sequence>
+	</complexType>
+</schema>
diff --git a/xsdconvert/ComplexType.cc b/xsdconvert/ComplexType.cc
index 130189a24991342fa8f41cf844505eac48c68bfb..9e0ddb9e5a21acc1e1956742d4110a1776237fbc 100644
--- a/xsdconvert/ComplexType.cc
+++ b/xsdconvert/ComplexType.cc
@@ -1616,7 +1616,8 @@ void ComplexType::resolveAttribute(AttributeType* attr) {
         if (st->getType().convertedValue == "record" || st->getType().convertedValue == "union") {
             st->addToNameDepList(attr);
           }
-        }
+      }
+      attr->getReference().set_resolved(st);
     } else {
       printError(module->getSchemaname(), name.convertedValue,
         "Reference for a non-defined type: " + attr->getReference().repr());
diff --git a/xsdconvert/GeneralFunctions.cc b/xsdconvert/GeneralFunctions.cc
index 4c7a4780b5801282171eff6fd01177014d94e967..d0c6e85e197c68f14bc85509946380b5d74f89b0 100644
--- a/xsdconvert/GeneralFunctions.cc
+++ b/xsdconvert/GeneralFunctions.cc
@@ -729,7 +729,10 @@ int multi(const TTCN3Module *module, ReferenceData const& outside_reference,
   RootType * ct = ::lookup1(module, outside_reference.get_val(), outside_reference.get_uri(), obj, want_CT);
   if (st || ct) {
     multiplicity = 1; // locally defined, no qualif needed
-  } else for (List<const TTCN3Module*>::iterator it = module->getImportedModules().begin(); it; it = it->Next) {
+    // means that outside_reference.get_uri() == module->getTargetNamespace())
+  } else {
+    // Look for definitions in the imported modules
+    for (List<const TTCN3Module*>::iterator it = module->getImportedModules().begin(); it; it = it->Next) {
       // Artificial lookup
       st = ::lookup1(it->Data, outside_reference.get_val(), it->Data->getTargetNamespace(), obj, want_ST);
       ct = ::lookup1(it->Data, outside_reference.get_val(), it->Data->getTargetNamespace(), obj, want_CT);
@@ -737,6 +740,17 @@ int multi(const TTCN3Module *module, ReferenceData const& outside_reference,
         ++multiplicity;
       }
     }
+    // If multiplicity > 1 then the qualif needed
+    // But if == 1 we need to check this module for a type definition with
+    // the same name as outsize_reference.get_val()
+    if (multiplicity == 1) {
+      st = ::lookup1(module, outside_reference.get_val(), module->getTargetNamespace(), obj, want_ST);
+      ct = ::lookup1(module, outside_reference.get_val(), module->getTargetNamespace(), obj, want_CT);
+      if (st || ct) {
+        ++multiplicity;
+      }
+    }
+  }
   return multiplicity;
 }
 
diff --git a/xsdconvert/SimpleType.cc b/xsdconvert/SimpleType.cc
index 5021f7e2ea420e1c4187ff1492d1215498d97845..fa3a7fdc6a44986c741a206960e4934d948fc4d8 100644
--- a/xsdconvert/SimpleType.cc
+++ b/xsdconvert/SimpleType.cc
@@ -775,18 +775,6 @@ void SimpleType::finalModification() {
   }
 
   isOptional = isOptional || (getMinOccurs() == 0 && getMaxOccurs() == 0);
-
-  // If the type name is the same as the identifier then we have to prefix it 
-  // with the module identifier.
-  if (type.convertedValue == name.convertedValue && !outside_reference.empty()) {
-    List<const TTCN3Module*>::iterator import_module = module->getImportedModules().begin();
-    for (; import_module; import_module = import_module->Next) {
-      if (import_module->Data->getTargetNamespace() == outside_reference.get_uri()) {
-        type.upload(import_module->Data->getModulename() + Mstring(".") + type.convertedValue);
-        break;
-      }
-    }
-  }
 }
 
 bool SimpleType::hasUnresolvedReference() {
@@ -835,8 +823,8 @@ void SimpleType::printToFile(FILE * file) {
 
     int multiplicity = multi(module, outside_reference, this);
     const RootType *type_ref = outside_reference.get_ref();
-    if ((multiplicity > 1) && type_ref
-      && type_ref->getModule() != module) {
+    if ((multiplicity > 1 && type_ref && type_ref->getModule() != module)
+       || name.convertedValue == type.convertedValue) {
       fprintf(file, "%s.", type_ref->getModule()->getModulename().c_str());
     }
 
diff --git a/xsdconvert/SimpleType.hh b/xsdconvert/SimpleType.hh
index 9a141048f83b09bf3f420ebfcd22f4ae2f6ae423..dc893650292b61b444aa521195187dc29fd017d9 100644
--- a/xsdconvert/SimpleType.hh
+++ b/xsdconvert/SimpleType.hh
@@ -331,7 +331,11 @@ public:
   const ReferenceData& getReference() const {
     return outside_reference;
   }
-
+  
+  ReferenceData& getReference() {
+    return outside_reference;
+  }
+    
   EnumerationType & getEnumeration() {
     return enumeration;
   }