Index: src/GenPoly/ScopedMap.h
===================================================================
--- src/GenPoly/ScopedMap.h	(revision 5e644d3ef72d65dc187491097854dec3ad90c16e)
+++ src/GenPoly/ScopedMap.h	(revision 46f61345ffd40c8a09b489da6e3b288fbf479cdf)
@@ -42,5 +42,5 @@
 		typedef typename Scope::pointer pointer;
 		typedef typename Scope::const_pointer const_pointer;
-		
+
 		class iterator : public std::iterator< std::bidirectional_iterator_tag,
 		                                       value_type > {
@@ -68,5 +68,5 @@
 			}
 
-			iterator(scope_list const &_scopes, const wrapped_iterator &_it, size_type _i)
+			iterator(scope_list &_scopes, const wrapped_iterator &_it, size_type _i)
 				: scopes(&_scopes), it(_it), i(_i) {}
 		public:
@@ -76,5 +76,5 @@
 				return *this;
 			}
-			
+
 			reference operator* () { return *it; }
 			pointer operator-> () { return it.operator->(); }
@@ -109,5 +109,5 @@
 
 		private:
-			scope_list const *scopes;
+			scope_list *scopes;
 			wrapped_iterator it;
 			size_type i;
@@ -189,9 +189,8 @@
 			size_type i;
 		};
-		
+
 		/// Starts a new scope
 		void beginScope() {
-			Scope scope;
-			scopes.push_back(scope);
+			scopes.emplace_back();
 		}
 
@@ -227,5 +226,5 @@
 				return const_iterator( const_cast< ScopedMap< Key, Value >* >(this)->find( key ) );
 		}
-		
+
 		/// Finds the given key in the outermost scope inside the given scope where it occurs
 		iterator findNext( const_iterator &it, const Key &key ) {
@@ -247,5 +246,12 @@
 			return std::make_pair( iterator(scopes, res.first, scopes.size()-1), res.second );
 		}
+
+		std::pair< iterator, bool > insert( value_type &&value ) {
+			std::pair< typename Scope::iterator, bool > res = scopes.back().insert( std::move( value ) );
+			return std::make_pair( iterator(scopes, std::move( res.first ), scopes.size()-1), std::move( res.second ) );
+		}
+
 		std::pair< iterator, bool > insert( const Key &key, const Value &value ) { return insert( std::make_pair( key, value ) ); }
+		std::pair< iterator, bool > insert( const Key &key, Value &&value ) { return insert( std::make_pair( key, std::move( value ) ) ); }
 
 		Value& operator[] ( const Key &key ) {
@@ -254,4 +260,25 @@
 			return insert( key, Value() ).first->second;
 		}
+
+		iterator erase( iterator pos ) {
+			Scope& scope = (*pos.scopes) [ pos.i ];
+			const typename iterator::wrapped_iterator& new_it = scope.erase( pos.it );
+			iterator it( *pos.scopes, new_it, pos.i );
+			return it.next_valid();
+		}
+
+		size_type count( const Key &key ) const {
+			size_type c = 0;
+			auto it = find( key );
+			auto end = cend();
+
+			while(it != end) {
+				c++;
+				it = findNext(it, key);
+			}
+
+			return c;
+		}
+
 	};
 } // namespace GenPoly
Index: src/SymTab/Validate.cc
===================================================================
--- src/SymTab/Validate.cc	(revision 5e644d3ef72d65dc187491097854dec3ad90c16e)
+++ src/SymTab/Validate.cc	(revision 46f61345ffd40c8a09b489da6e3b288fbf479cdf)
@@ -49,4 +49,5 @@
 #include "SynTree/Statement.h"
 #include "SynTree/TypeSubstitution.h"
+#include "GenPoly/ScopedMap.h"
 #include "Indexer.h"
 #include "FixFunction.h"
@@ -162,5 +163,6 @@
 		void addImplicitTypedef( AggDecl * aggDecl );
 
-		typedef std::map< std::string, std::pair< TypedefDecl *, int > > TypedefMap;
+		typedef std::unique_ptr<TypedefDecl> TypedefDeclPtr;
+		typedef GenPoly::ScopedMap< std::string, std::pair< TypedefDeclPtr, int > > TypedefMap;
 		typedef std::map< std::string, TypeDecl * > TypeDeclMap;
 		TypedefMap typedefNames;
@@ -549,5 +551,5 @@
 			}
 		} else {
-			typedefNames[ tyDecl->get_name() ] = std::make_pair( tyDecl, scopeLevel );
+			typedefNames[ tyDecl->get_name() ] = std::make_pair( TypedefDeclPtr( tyDecl ), scopeLevel );
 		} // if
 
@@ -567,5 +569,5 @@
 			return new EnumDecl( enumDecl->get_name() );
 		} else {
-			return ret;
+			return ret->clone();
 		} // if
 	}
@@ -582,14 +584,14 @@
 
 	DeclarationWithType *EliminateTypedef::mutate( FunctionDecl * funcDecl ) {
-		TypedefMap oldNames = typedefNames;
+		typedefNames.beginScope();
 		DeclarationWithType *ret = Mutator::mutate( funcDecl );
-		typedefNames = oldNames;
+		typedefNames.endScope();
 		return ret;
 	}
 
 	DeclarationWithType *EliminateTypedef::mutate( ObjectDecl * objDecl ) {
-		TypedefMap oldNames = typedefNames;
+		typedefNames.beginScope();
 		DeclarationWithType *ret = Mutator::mutate( objDecl );
-		typedefNames = oldNames;
+		typedefNames.endScope();
 		// is the type a function?
 		if ( FunctionType *funtype = dynamic_cast<FunctionType *>( ret->get_type() ) ) {
@@ -603,12 +605,12 @@
 
 	Expression *EliminateTypedef::mutate( CastExpr * castExpr ) {
-		TypedefMap oldNames = typedefNames;
+		typedefNames.beginScope();
 		Expression *ret = Mutator::mutate( castExpr );
-		typedefNames = oldNames;
+		typedefNames.endScope();
 		return ret;
 	}
 
 	CompoundStmt *EliminateTypedef::mutate( CompoundStmt * compoundStmt ) {
-		TypedefMap oldNames = typedefNames;
+		typedefNames.beginScope();
 		scopeLevel += 1;
 		CompoundStmt *ret = Mutator::mutate( compoundStmt );
@@ -625,5 +627,5 @@
 			i = next;
 		} // while
-		typedefNames = oldNames;
+		typedefNames.endScope();
 		return ret;
 	}
@@ -656,6 +658,6 @@
 				type = new EnumInstType( Type::Qualifiers(), newDeclEnumDecl->get_name() );
 			} // if
-			TypedefDecl * tyDecl = new TypedefDecl( aggDecl->get_name(), DeclarationNode::NoStorageClass, type );
-			typedefNames[ aggDecl->get_name() ] = std::make_pair( tyDecl, scopeLevel );
+			TypedefDeclPtr tyDecl( new TypedefDecl( aggDecl->get_name(), DeclarationNode::NoStorageClass, type ) );
+			typedefNames[ aggDecl->get_name() ] = std::make_pair( std::move( tyDecl ), scopeLevel );
 		} // if
 	}
Index: src/SynTree/Declaration.h
===================================================================
--- src/SynTree/Declaration.h	(revision 5e644d3ef72d65dc187491097854dec3ad90c16e)
+++ src/SynTree/Declaration.h	(revision 46f61345ffd40c8a09b489da6e3b288fbf479cdf)
@@ -153,5 +153,5 @@
   public:
 	NamedTypeDecl( const std::string &name, DeclarationNode::StorageClass sc, Type *type );
-	NamedTypeDecl( const TypeDecl &other );
+	NamedTypeDecl( const NamedTypeDecl &other );
 	virtual ~NamedTypeDecl();
 
Index: src/SynTree/NamedTypeDecl.cc
===================================================================
--- src/SynTree/NamedTypeDecl.cc	(revision 5e644d3ef72d65dc187491097854dec3ad90c16e)
+++ src/SynTree/NamedTypeDecl.cc	(revision 46f61345ffd40c8a09b489da6e3b288fbf479cdf)
@@ -5,5 +5,5 @@
 // file "LICENCE" distributed with Cforall.
 //
-// NamedTypeDecl.cc -- 
+// NamedTypeDecl.cc --
 //
 // Author           : Richard C. Bilson
@@ -21,5 +21,5 @@
 	: Parent( name, sc, LinkageSpec::Cforall ), base( base ) {}
 
-NamedTypeDecl::NamedTypeDecl( const TypeDecl &other )
+NamedTypeDecl::NamedTypeDecl( const NamedTypeDecl &other )
 	: Parent( other ), base( maybeClone( other.base ) ) {
 	cloneAll( other.parameters, parameters );
@@ -35,5 +35,5 @@
 void NamedTypeDecl::print( std::ostream &os, int indent ) const {
 	using namespace std;
-	
+
 	if ( get_name() != "" ) {
 		os << get_name() << ": ";
@@ -59,5 +59,5 @@
 void NamedTypeDecl::printShort( std::ostream &os, int indent ) const {
 	using namespace std;
-	
+
 	if ( get_name() != "" ) {
 		os << get_name() << ": ";
Index: src/main.cc
===================================================================
--- src/main.cc	(revision 5e644d3ef72d65dc187491097854dec3ad90c16e)
+++ src/main.cc	(revision 46f61345ffd40c8a09b489da6e3b288fbf479cdf)
@@ -84,5 +84,5 @@
 		 << " backtrace:" << endl;
 
-	char ** messages = backtrace_symbols( array, size );    
+	char ** messages = backtrace_symbols( array, size );
 
 	// skip first stack frame (points here)
@@ -91,5 +91,5 @@
 		for ( char *p = messages[i]; *p; ++p ) {        // find parantheses and +offset
 			if (*p == '(') {
-				mangled_name = p; 
+				mangled_name = p;
 			} else if (*p == '+') {
 				offset_begin = p;
@@ -109,9 +109,9 @@
 			char * real_name = __cxxabiv1::__cxa_demangle( mangled_name, 0, 0, &status );
 			if ( status == 0 ) {						// demangling successful ?
-				cerr << "(" << i - 2 << ") " << messages[i] << " : " 
+				cerr << "(" << i - 2 << ") " << messages[i] << " : "
 					 << real_name << "+" << offset_begin << offset_end << endl;
 
 			} else {									// otherwise, output mangled name
-				cerr << "(" << i - 2 << ") " << messages[i] << " : " 
+				cerr << "(" << i - 2 << ") " << messages[i] << " : "
 					 << mangled_name << "+" << offset_begin << offset_end << endl;
 			} // if
@@ -198,4 +198,5 @@
 		SymTab::validate( translationUnit, symtabp );
 		if ( symtabp ) {
+			deleteAll( translationUnit );
 			return 0;
 		} // if
