Index: src/GenPoly/ScopedMap.h
===================================================================
--- src/GenPoly/ScopedMap.h	(revision 6f49cdf3c1329b4fdca21be3f751faf62a3ee0b2)
+++ src/GenPoly/ScopedMap.h	(revision 5ba653c375e2294bf122e14010ce19e3f5bf5358)
@@ -51,4 +51,21 @@
 			typedef typename scope_list::size_type size_type;
 
+			/// Checks if this iterator points to a valid item
+			bool is_valid() const {
+				return it != (*scopes)[i].end();
+			}
+
+			/// Increments on invalid
+			iterator& next_valid() {
+				if ( ! is_valid() ) { ++(*this); }
+				return *this;
+			}
+
+			/// Decrements on invalid
+			iterator& prev_valid() {
+				if ( ! is_valid() ) { --(*this); }
+				return *this;
+			}
+
 			iterator(scope_list const &_scopes, const wrapped_iterator &_it, size_type _i)
 				: scopes(&_scopes), it(_it), i(_i) {}
@@ -68,8 +85,8 @@
 					--i;
 					it = (*scopes)[i].begin();
-					return *this;
-				}
-				++it;
-				return *this;
+				} else {
+					++it;
+				}
+				return next_valid();
 			}
 			iterator& operator++ (int) { iterator tmp = *this; ++(*this); return tmp; }
@@ -82,5 +99,5 @@
 				}
 				--it;
-				return *this;
+				return prev_valid();
 			}
 			iterator& operator-- (int) { iterator tmp = *this; --(*this); return tmp; }
@@ -105,4 +122,21 @@
 			typedef typename scope_list::size_type size_type;
 
+			/// Checks if this iterator points to a valid item
+			bool is_valid() const {
+				return it != (*scopes)[i].end();
+			}
+
+			/// Increments on invalid
+			const_iterator& next_valid() {
+				if ( ! is_valid() ) { ++(*this); }
+				return *this;
+			}
+
+			/// Decrements on invalid
+			const_iterator& prev_valid() {
+				if ( ! is_valid() ) { --(*this); }
+				return *this;
+			}
+
 			const_iterator(scope_list const &_scopes, const wrapped_const_iterator &_it, size_type _i)
 				: scopes(&_scopes), it(_it), i(_i) {}
@@ -127,8 +161,8 @@
 					--i;
 					it = (*scopes)[i].begin();
-					return *this;
-				}
-				++it;
-				return *this;
+				} else {
+					++it;
+				}
+				return next_valid();
 			}
 			const_iterator& operator++ (int) { const_iterator tmp = *this; ++(*this); return tmp; }
@@ -141,5 +175,5 @@
 				}
 				--it;
-				return *this;
+				return prev_valid();
 			}
 			const_iterator& operator-- (int) { const_iterator tmp = *this; --(*this); return tmp; }
@@ -171,7 +205,7 @@
 		ScopedMap() { beginScope(); }
 
-		iterator begin() { return iterator(scopes, scopes.back().begin(), scopes.size()-1); }
-		const_iterator begin() const { return const_iterator(scopes, scopes.back().begin(), scopes.size()-1); }
-		const_iterator cbegin() const { return const_iterator(scopes, scopes.back().begin(), scopes.size()-1); }
+		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 end() { return iterator(scopes, scopes[0].end(), 0); }
 		const_iterator end() const { return const_iterator(scopes, scopes[0].end(), 0); }
Index: src/GenPoly/ScopedSet.h
===================================================================
--- src/GenPoly/ScopedSet.h	(revision 6f49cdf3c1329b4fdca21be3f751faf62a3ee0b2)
+++ src/GenPoly/ScopedSet.h	(revision 5ba653c375e2294bf122e14010ce19e3f5bf5358)
@@ -48,4 +48,21 @@
 			typedef typename scope_list::size_type size_type;
 
+			/// Checks if this iterator points to a valid item
+			bool is_valid() const {
+				return it != (*scopes)[i].end();
+			}
+
+			/// Increments on invalid
+			iterator& next_valid() {
+				if ( ! is_valid() ) { ++(*this); }
+				return *this;
+			}
+
+			/// Decrements on invalid
+			iterator& prev_valid() {
+				if ( ! is_valid() ) { --(*this); }
+				return *this;
+			}
+
 			iterator(scope_list const &_scopes, const wrapped_iterator &_it, size_type _i)
 				: scopes(&_scopes), it(_it), i(_i) {}
@@ -65,8 +82,8 @@
 					--i;
 					it = (*scopes)[i].begin();
-					return *this;
-				}
-				++it;
-				return *this;
+				} else {
+					++it;
+				}
+				return next_valid();
 			}
 			iterator& operator++ (int) { iterator tmp = *this; ++(*this); return tmp; }
@@ -79,5 +96,5 @@
 				}
 				--it;
-				return *this;
+				return prev_valid();
 			}
 			iterator& operator-- (int) { iterator tmp = *this; --(*this); return tmp; }
@@ -102,4 +119,21 @@
 			typedef typename scope_list::size_type size_type;
 
+			/// Checks if this iterator points to a valid item
+			bool is_valid() const {
+				return it != (*scopes)[i].end();
+			}
+
+			/// Increments on invalid
+			const_iterator& next_valid() {
+				if ( ! is_valid() ) { ++(*this); }
+				return *this;
+			}
+
+			/// Decrements on invalid
+			const_iterator& prev_valid() {
+				if ( ! is_valid() ) { --(*this); }
+				return *this;
+			}
+
 			const_iterator(scope_list const &_scopes, const wrapped_const_iterator &_it, size_type _i)
 				: scopes(&_scopes), it(_it), i(_i) {}
@@ -124,8 +158,8 @@
 					--i;
 					it = (*scopes)[i].begin();
-					return *this;
-				}
-				++it;
-				return *this;
+				} else {
+					++it;
+				}
+				return next_valid();
 			}
 			const_iterator& operator++ (int) { const_iterator tmp = *this; ++(*this); return tmp; }
@@ -138,5 +172,5 @@
 				}
 				--it;
-				return *this;
+				return prev_valid();
 			}
 			const_iterator& operator-- (int) { const_iterator tmp = *this; --(*this); return tmp; }
@@ -167,7 +201,7 @@
 		ScopedSet() { beginScope(); }
 
-		iterator begin() { return iterator(scopes, scopes.back().begin(), scopes.size()-1); }
-		const_iterator begin() const { return const_iterator(scopes, scopes.back().begin(), scopes.size()-1); }
-		const_iterator cbegin() const { return const_iterator(scopes, scopes.back().begin(), scopes.size()-1); }
+		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 end() { return iterator(scopes, scopes[0].end(), 0); }
 		const_iterator end() const { return const_iterator(scopes, scopes[0].end(), 0); }
