diff --git a/src/libs/cplusplus/CppRewriter.cpp b/src/libs/cplusplus/CppRewriter.cpp
index 9b28a8102c9fbb5d332f7b301ddaaa7f6b0dcffa..26c11596ddba3e3e1cdc9279ef4053788c587f8e 100644
--- a/src/libs/cplusplus/CppRewriter.cpp
+++ b/src/libs/cplusplus/CppRewriter.cpp
@@ -34,7 +34,6 @@
 #include <Literals.h>
 #include <Names.h>
 #include <Scope.h>
-#include <cplusplus/Overview.h>
 
 #include <QtCore/QVarLengthArray>
 #include <QtCore/QDebug>
@@ -121,7 +120,7 @@ public:
         {
             FullySpecifiedType ty = rewrite->env->apply(type->name(), rewrite);
             if (! ty->isUndefinedType())
-                temps.append(ty);
+                temps.append(rewrite->rewriteType(ty));
             else {
                 const Name *name = rewrite->rewriteName(type->name());
                 temps.append(control()->namedType(name));
@@ -287,57 +286,39 @@ public: // attributes
     RewriteName rewriteName;
 };
 
-SubstitutionEnvironment::SubstitutionEnvironment()
-    : _scope(0)
+ContextSubstitution::ContextSubstitution(const LookupContext &context, Scope *scope)
+    : _context(context), _scope(scope)
 {
 }
 
-FullySpecifiedType SubstitutionEnvironment::apply(const Name *name, Rewrite *rewrite) const
+ContextSubstitution::~ContextSubstitution()
 {
-    if (name) {
-        for (int index = _substs.size() - 1; index != -1; --index) {
-            const Substitution *subst = _substs.at(index);
-
-            FullySpecifiedType ty = subst->apply(name, rewrite);
-            if (! ty->isUndefinedType())
-                return ty;
-        }
-    }
-
-    return FullySpecifiedType();
 }
 
-void SubstitutionEnvironment::enter(Substitution *subst)
+FullySpecifiedType ContextSubstitution::apply(const Name *name, Rewrite *rewrite) const
 {
-    _substs.append(subst);
-}
+    const QList<LookupItem> candidates = _context.lookup(name, _scope);
 
-void SubstitutionEnvironment::leave()
-{
-    _substs.removeLast();
-}
+    foreach (const LookupItem &r, candidates) {
+        Symbol *s = r.declaration();
+        if (s->isDeclaration() && s->isTypedef()) {
+            qDebug() << "resolved typedef:" << s->fileName() << s->line() << s->column();
 
-Scope *SubstitutionEnvironment::scope() const
-{
-    return _scope;
-}
+            qDebug() << "scope is:" << r.scope()->owner()->fileName()
+                     << r.scope()->owner()->line()
+                     << r.scope()->owner()->column();
 
-Scope *SubstitutionEnvironment::switchScope(Scope *scope)
-{
-    Scope *previous = _scope;
-    _scope = scope;
-    return previous;
-}
+            ContextSubstitution subst(_context, s->scope());
+            rewrite->env->enter(&subst);
+            FullySpecifiedType ty = rewrite->rewriteType(s->type());
+            rewrite->env->leave();
 
-const LookupContext &SubstitutionEnvironment::context() const
-{
-    return _context;
+            return ty;
+        }
+    }
+    return FullySpecifiedType();
 }
 
-void SubstitutionEnvironment::setContext(const LookupContext &context)
-{
-    _context = context;
-}
 
 SubstitutionMap::SubstitutionMap()
 {
@@ -366,49 +347,6 @@ FullySpecifiedType SubstitutionMap::apply(const Name *name, Rewrite *) const
     return FullySpecifiedType();
 }
 
-
-UseQualifiedNames::UseQualifiedNames()
-{
-
-}
-
-UseQualifiedNames::~UseQualifiedNames()
-{
-
-}
-
-FullySpecifiedType UseQualifiedNames::apply(const Name *name, Rewrite *rewrite) const
-{
-    SubstitutionEnvironment *env = rewrite->env;
-    Scope *scope = env->scope();
-
-    if (! scope)
-        return FullySpecifiedType();
-
-    const LookupContext &context = env->context();
-    Control *control = rewrite->control;
-
-    const QList<LookupItem> results = context.lookup(name, scope);
-    foreach (const LookupItem &r, results) {
-        if (Symbol *d = r.declaration()) {
-            const Name *n = 0;
-            foreach (const Name *c,  LookupContext::fullyQualifiedName(d)) {
-                if (! n)
-                    n = c;
-                else
-                    n = control->qualifiedNameId(n, c);
-            }
-
-            return control->namedType(n);
-        }
-
-        return r.type();
-    }
-
-    return FullySpecifiedType();
-}
-
-
 FullySpecifiedType CPlusPlus::rewriteType(const FullySpecifiedType &type,
                                           SubstitutionEnvironment *env,
                                           Control *control)
diff --git a/src/libs/cplusplus/CppRewriter.h b/src/libs/cplusplus/CppRewriter.h
index 988b92f3a624dd58d3157152aa5b4c7626617a48..0e210611281e4eaee8e2919e84c2ab0fa07b08ea 100644
--- a/src/libs/cplusplus/CppRewriter.h
+++ b/src/libs/cplusplus/CppRewriter.h
@@ -51,27 +51,51 @@ public:
 class CPLUSPLUS_EXPORT SubstitutionEnvironment
 {
     Q_DISABLE_COPY(SubstitutionEnvironment)
+    QList<Substitution *> substs;
 
 public:
-    SubstitutionEnvironment();
-
-    FullySpecifiedType apply(const Name *name, Rewrite *rewrite) const;
-
-    void enter(Substitution *subst);
-    void leave();
+    SubstitutionEnvironment() {}
+
+    FullySpecifiedType apply(const Name *name, Rewrite *rewrite) const
+    {
+        if (name) {
+            for (int index = substs.size() - 1; index != -1; --index) {
+                const Substitution *subst = substs.at(index);
+
+                FullySpecifiedType ty = subst->apply(name, rewrite);
+                if (! ty->isUndefinedType())
+                    return ty;
+            }
+        }
+
+        return FullySpecifiedType();
+    }
+
+    void enter(Substitution *subst)
+    {
+        substs.append(subst);
+    }
+
+    void leave()
+    {
+        substs.removeLast();
+    }
+};
 
-    Scope *scope() const;
-    Scope *switchScope(Scope *scope);
+class CPLUSPLUS_EXPORT ContextSubstitution: public Substitution
+{
+public:
+    ContextSubstitution(const LookupContext &context, Scope *scope);
+    virtual ~ContextSubstitution();
 
-    const LookupContext &context() const;
-    void setContext(const LookupContext &context);
+    virtual FullySpecifiedType apply(const Name *name, Rewrite *rewrite) const;
 
 private:
-    QList<Substitution *> _substs;
-    Scope *_scope;
     LookupContext _context;
+    Scope *_scope;
 };
 
+
 class CPLUSPLUS_EXPORT SubstitutionMap: public Substitution
 {
 public:
@@ -85,17 +109,6 @@ private:
     QList<QPair<const Name *, FullySpecifiedType> > _map;
 };
 
-class CPLUSPLUS_EXPORT UseQualifiedNames: public Substitution
-{
-public:
-    UseQualifiedNames();
-    virtual ~UseQualifiedNames();
-
-    virtual FullySpecifiedType apply(const Name *name, Rewrite *rewrite) const;
-};
-
-
-
 CPLUSPLUS_EXPORT FullySpecifiedType rewriteType(const FullySpecifiedType &type,
                                                 SubstitutionEnvironment *env,
                                                 Control *control);
diff --git a/src/libs/cplusplus/ResolveExpression.cpp b/src/libs/cplusplus/ResolveExpression.cpp
index 8bbdb8f05a0e2995efc925d6fae674559ee22d7a..651af198717ce0e8eda5934b19faf716b17eb788 100644
--- a/src/libs/cplusplus/ResolveExpression.cpp
+++ b/src/libs/cplusplus/ResolveExpression.cpp
@@ -568,10 +568,9 @@ QList<LookupItem> ResolveExpression::getMembers(ClassOrNamespace *binding, const
                 }
 
                 SubstitutionEnvironment env;
-                if (m.scope())
-                    env.switchScope(m.scope());
-                env.setContext(_context);
+                ContextSubstitution ctxSubst(_context, m.scope());
 
+                env.enter(&ctxSubst);
                 env.enter(&map);
                 FullySpecifiedType instantiatedTy = rewriteType(decl->type(), &env, _context.control().data());
 
diff --git a/src/plugins/cppeditor/cppquickfix.cpp b/src/plugins/cppeditor/cppquickfix.cpp
index fdc9d676ebcada2a5db19b501c5fd8da1e25aa19..eaa7600c16cd5bbcbe91f29daea190ba79cae6de 100644
--- a/src/plugins/cppeditor/cppquickfix.cpp
+++ b/src/plugins/cppeditor/cppquickfix.cpp
@@ -37,7 +37,6 @@
 #include <cplusplus/Overview.h>
 #include <cplusplus/TypeOfExpression.h>
 #include <cplusplus/DependencyTable.h>
-#include <cplusplus/CppRewriter.h>
 
 #include <TranslationUnit.h>
 #include <ASTVisitor.h>
@@ -1547,18 +1546,8 @@ public:
                                                           TypeOfExpression::Preprocess);
 
         if (! result.isEmpty()) {
-
-            SubstitutionEnvironment env;
-            env.setContext(context());
-            env.switchScope(result.first().scope());
-            UseQualifiedNames q;
-            env.enter(&q);
-
-            Control *control = context().control().data();
-            FullySpecifiedType tn = rewriteType(result.first().type(), &env, control);
-
             Overview oo;
-            QString ty = oo(tn);
+            QString ty = oo(result.first().type());
             if (! ty.isEmpty()) {
                 const QChar ch = ty.at(ty.size() - 1);