Index: src/Common/ScopedMap.h
===================================================================
--- src/Common/ScopedMap.h	(revision e491159c00c09ae82c7b254c1c76fba21a6d2d5c)
+++ src/Common/ScopedMap.h	(revision 1f75e2dfc9fb42d253c296ec968fba766b59aa29)
@@ -52,5 +52,5 @@
 		/// Checks if this iterator points to a valid item
 		bool is_valid() const {
-			return it != (*scopes)[i].end();
+			return it != (*scopes)[level].end();
 		}
 
@@ -67,10 +67,10 @@
 		}
 
-		iterator(scope_list &_scopes, const wrapped_iterator &_it, size_type _i)
-			: scopes(&_scopes), it(_it), i(_i) {}
+		iterator(scope_list &_scopes, const wrapped_iterator &_it, size_type inLevel)
+			: scopes(&_scopes), it(_it), level(inLevel) {}
 	public:
-		iterator(const iterator &that) : scopes(that.scopes), it(that.it), i(that.i) {}
+		iterator(const iterator &that) : scopes(that.scopes), it(that.it), level(that.level) {}
 		iterator& operator= (const iterator &that) {
-			scopes = that.scopes; i = that.i; it = that.it;
+			scopes = that.scopes; level = that.level; it = that.it;
 			return *this;
 		}
@@ -80,8 +80,8 @@
 
 		iterator& operator++ () {
-			if ( it == (*scopes)[i].end() ) {
-				if ( i == 0 ) return *this;
-				--i;
-				it = (*scopes)[i].begin();
+			if ( it == (*scopes)[level].end() ) {
+				if ( level == 0 ) return *this;
+				--level;
+				it = (*scopes)[level].begin();
 			} else {
 				++it;
@@ -93,7 +93,7 @@
 		iterator& operator-- () {
 			// may fail if this is the begin iterator; allowed by STL spec
-			if ( it == (*scopes)[i].begin() ) {
-				++i;
-				it = (*scopes)[i].end();
+			if ( it == (*scopes)[level].begin() ) {
+				++level;
+				it = (*scopes)[level].end();
 			}
 			--it;
@@ -103,12 +103,14 @@
 
 		bool operator== (const iterator &that) {
-			return scopes == that.scopes && i == that.i && it == that.it;
+			return scopes == that.scopes && level == that.level && it == that.it;
 		}
 		bool operator!= (const iterator &that) { return !( *this == that ); }
+
+		size_type get_level() const { return level; }
 
 	private:
 		scope_list *scopes;
 		wrapped_iterator it;
-		size_type i;
+		size_type level;
 	};
 
@@ -123,5 +125,5 @@
 		/// Checks if this iterator points to a valid item
 		bool is_valid() const {
-			return it != (*scopes)[i].end();
+			return it != (*scopes)[level].end();
 		}
 
@@ -138,15 +140,15 @@
 		}
 
-		const_iterator(scope_list const &_scopes, const wrapped_const_iterator &_it, size_type _i)
-			: scopes(&_scopes), it(_it), i(_i) {}
+		const_iterator(scope_list const &_scopes, const wrapped_const_iterator &_it, size_type inLevel)
+			: scopes(&_scopes), it(_it), level(inLevel) {}
 	public:
-		const_iterator(const iterator &that) : scopes(that.scopes), it(that.it), i(that.i) {}
-		const_iterator(const const_iterator &that) : scopes(that.scopes), it(that.it), i(that.i) {}
+		const_iterator(const iterator &that) : scopes(that.scopes), it(that.it), level(that.level) {}
+		const_iterator(const const_iterator &that) : scopes(that.scopes), it(that.it), level(that.level) {}
 		const_iterator& operator= (const iterator &that) {
-			scopes = that.scopes; i = that.i; it = that.it;
+			scopes = that.scopes; level = that.level; it = that.it;
 			return *this;
 		}
 		const_iterator& operator= (const const_iterator &that) {
-			scopes = that.scopes; i = that.i; it = that.it;
+			scopes = that.scopes; level = that.level; it = that.it;
 			return *this;
 		}
@@ -156,8 +158,8 @@
 
 		const_iterator& operator++ () {
-			if ( it == (*scopes)[i].end() ) {
-				if ( i == 0 ) return *this;
-				--i;
-				it = (*scopes)[i].begin();
+			if ( it == (*scopes)[level].end() ) {
+				if ( level == 0 ) return *this;
+				--level;
+				it = (*scopes)[level].begin();
 			} else {
 				++it;
@@ -169,7 +171,7 @@
 		const_iterator& operator-- () {
 			// may fail if this is the begin iterator; allowed by STL spec
-			if ( it == (*scopes)[i].begin() ) {
-				++i;
-				it = (*scopes)[i].end();
+			if ( it == (*scopes)[level].begin() ) {
+				++level;
+				it = (*scopes)[level].end();
 			}
 			--it;
@@ -179,12 +181,14 @@
 
 		bool operator== (const const_iterator &that) {
-			return scopes == that.scopes && i == that.i && it == that.it;
+			return scopes == that.scopes && level == that.level && it == that.it;
 		}
 		bool operator!= (const const_iterator &that) { return !( *this == that ); }
+
+		size_type get_level() const { return level; }
 
 	private:
 		scope_list const *scopes;
 		wrapped_const_iterator it;
-		size_type i;
+		size_type level;
 	};
 
@@ -203,7 +207,7 @@
 	ScopedMap() { beginScope(); }
 
-	iterator begin() { return iterator(scopes, scopes.back().begin(), scopes.size()-1).next_valid(); }
-	const_iterator begin() const { return const_iterator(scopes, scopes.back().begin(), scopes.size()-1).next_valid(); }
-	const_iterator cbegin() const { return const_iterator(scopes, scopes.back().begin(), scopes.size()-1).next_valid(); }
+	iterator begin() { return iterator(scopes, scopes.back().begin(), currentScope()).next_valid(); }
+	const_iterator begin() const { return const_iterator(scopes, scopes.back().begin(), currentScope()).next_valid(); }
+	const_iterator cbegin() const { return const_iterator(scopes, scopes.back().begin(), currentScope()).next_valid(); }
 	iterator end() { return iterator(scopes, scopes[0].end(), 0); }
 	const_iterator end() const { return const_iterator(scopes, scopes[0].end(), 0); }
@@ -211,5 +215,5 @@
 
 	/// Gets the index of the current scope (counted from 1)
-	size_type currentScope() const { return scopes.size(); }
+	size_type currentScope() const { return scopes.size() - 1; }
 
 	/// Finds the given key in the outermost scope it occurs; returns end() for none such
@@ -228,6 +232,6 @@
 	/// Finds the given key in the outermost scope inside the given scope where it occurs
 	iterator findNext( const_iterator &it, const Key &key ) {
-		if ( it.i == 0 ) return end();
-		for ( size_type i = it.i - 1; ; --i ) {
+		if ( it.level == 0 ) return end();
+		for ( size_type i = it.level - 1; ; --i ) {
 			typename Scope::iterator val = scopes[i].find( key );
 			if ( val != scopes[i].end() ) return iterator( scopes, val, i );
@@ -241,16 +245,25 @@
 
 	/// Inserts the given key-value pair into the outermost scope
-	std::pair< iterator, bool > insert( const value_type &value ) {
-		std::pair< typename Scope::iterator, bool > res = scopes.back().insert( value );
-		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 ) );
+	template< typename value_type_t >
+	std::pair< iterator, bool > insert( value_type_t&& value ) {
+		std::pair< typename Scope::iterator, bool > res = scopes.back().insert( std::forward<value_type_t>( 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 ) ) ); }
+	template< typename value_type_t >
+	std::pair< iterator, bool > insert( iterator at, value_type_t&& value ) {
+		Scope& scope = (*at.scopes) [ at.level ];
+		std::pair< typename Scope::iterator, bool > res = scope.insert( std::forward<value_type_t>( value ) );
+		return std::make_pair( iterator(scopes, std::move( res.first ), at.level), std::move( res.second ) );
+	}
+
+	template< typename value_t >
+	std::pair< iterator, bool > insert( const Key &key, value_t&& value ) { return insert( std::make_pair( key, std::forward<value_t>( value ) ) ); }
+
+	template< typename value_type_t >
+	std::pair< iterator, bool > insertAt( size_type scope, value_type_t&& value ) {
+		std::pair< typename Scope::iterator, bool > res = scopes.at(scope).insert( std::forward<value_type_t>( value ) );
+		return std::make_pair( iterator(scopes, std::move( res.first ), scope), std::move( res.second ) );
+	}
 
 	Value& operator[] ( const Key &key ) {
@@ -261,7 +274,7 @@
 
 	iterator erase( iterator pos ) {
-		Scope& scope = (*pos.scopes) [ pos.i ];
+		Scope& scope = (*pos.scopes) [ pos.level ];
 		const typename iterator::wrapped_iterator& new_it = scope.erase( pos.it );
-		iterator it( *pos.scopes, new_it, pos.i );
+		iterator it( *pos.scopes, new_it, pos.level );
 		return it.next_valid();
 	}
Index: src/Common/utility.h
===================================================================
--- src/Common/utility.h	(revision e491159c00c09ae82c7b254c1c76fba21a6d2d5c)
+++ src/Common/utility.h	(revision 1f75e2dfc9fb42d253c296ec968fba766b59aa29)
@@ -262,4 +262,7 @@
 };
 
+template< typename ThisType >
+std::weak_ptr<ThisType> RefCountSingleton<ThisType>::global_instance;
+
 #endif // _UTILITY_H
 
