Index: trunk/nv.lua
===================================================================
--- trunk/nv.lua	(revision 550)
+++ trunk/nv.lua	(revision 551)
@@ -112,14 +112,16 @@
 	links { "nv-core", "nv-lib", "nv-io", "nv-gfx", "nv-lua" }
 
+--[[
 -- injection!
 solution( solution().name )
 	configuration "gmake"
-		if _ACTION == "gmake-clang" then
+		if CLANG then
 			buildoptions { 
-				"-std=c++1z",
+				"-std=c++14",
 --                                -- on Mac OS X don't try to use old stdc++
 --                                "-stdlib=libc++",
-				"-Weverything",
+--				"-Weverything",
 				-- math is so much easier with these
+				"-w",
 				"-Wno-gnu-anonymous-struct", 
 				"-Wno-nested-anon-types", 
@@ -163,6 +165,5 @@
 
 if _ACTION == "gmake-clang" then
-	premake.gcc.cc  = "clang"
-	premake.gcc.cxx = "clang++"
+	toolset "clang"
 	_ACTION = "gmake"
 end
@@ -178,2 +179,4 @@
 	end
 end
+
+--]]
Index: trunk/nv/base/common.hh
===================================================================
--- trunk/nv/base/common.hh	(revision 550)
+++ trunk/nv/base/common.hh	(revision 551)
@@ -184,4 +184,8 @@
 #endif
 
+#if NV_COMPILER != NV_MSVC 
+#define NULL 0
+#endif
+
 namespace nv
 {
Index: trunk/nv/core/resource.hh
===================================================================
--- trunk/nv/core/resource.hh	(revision 550)
+++ trunk/nv/core/resource.hh	(revision 551)
@@ -221,5 +221,5 @@
 			shash64 hid( id );
 			if ( m->second->exists( hid ) || m->second->load_resource( id ) )
-				return m->second->create< T >( hid );
+				return m->second->template create< T >( hid );
 			NV_ASSERT( false, "resource_manager.get failed!" );
 			return resource< T >();
@@ -234,5 +234,5 @@
 			if ( m->second->exists( hid ) )
 			{
-				return m->second->create< T >( hid );
+				return m->second->template create< T >( hid );
 			}
 			NV_ASSERT( false, "resource_manager.get failed!" );
@@ -246,5 +246,5 @@
 			NV_ASSERT( m != m_handlers.end(), "Resource type unrecognized!" );
 			m->second->raw_add( id, value );
-			return m->second->create< T >( id );
+			return m->second->template create< T >( id );
 		}
 
Index: trunk/nv/ecs/component.hh
===================================================================
--- trunk/nv/ecs/component.hh	(revision 550)
+++ trunk/nv/ecs/component.hh	(revision 551)
@@ -37,5 +37,5 @@
 			typename Component,
 			typename Handler,
-			template < typename, typename > class IndexTable = index_table
+			template < typename, typename > class IndexTable = flat_index_table
 		>
 		class component : public Ecs::component_interface
@@ -47,4 +47,5 @@
 			typedef typename ecs_type::time_type            time_type;
 			typedef IndexTable< handle_type, sint32 >       index_table_type;
+			typedef index_table< handle_type, sint32 >      idx_table;
 			typedef Component                               value_type;
 			typedef Component                               component_type;
@@ -58,18 +59,23 @@
 			typedef typename storage_type::const_reference  const_reference;
 
-			component( ecs_type& a_ecs, string_view a_name, bool relational = false, uint32 reserve = 0 ) 
+			component( ecs_type& a_ecs, string_view a_name, bool relational = false ) 
 				: m_ecs( a_ecs ), m_relational( relational ), m_storage( nullptr )
 			{
-				m_storage = new storage_type;
-				m_ecs.register_component<component_type, Handler>( a_name, this );
-				if ( reserve != 0 )
-				{
-					m_storage->reserve( reserve );
-				}
+				m_index = new index_table_type;
+				m_storage = (storage_type*)m_ecs.template register_component<component_type, Handler>( a_name, this );
+// 				if ( reserve != 0 )
+// 				{
+// 					m_storage->reserve( reserve );
+// 				}
+			}
+
+			void temp_storage( storage_type* storage )
+			{
+
 			}
 
 			virtual uint32 temp_index( handle_type h ) const  final
 			{
-				return m_index.get( h );
+				return m_index->get( h );
 			}
 
@@ -77,5 +83,5 @@
 			inline value_type& insert( handle_type h )
 			{
-				index_type i = m_index.insert( h );
+				index_type i = m_index->insert( h );
 				NV_ASSERT( i == index_type( m_storage->size() ), "Fail!" );
 				NV_UNUSED( i );
@@ -87,5 +93,5 @@
 			value_type& insert( handle_type h, Args&&... args )
 			{
-				index_type i = m_index.insert( h );
+				index_type i = m_index->insert( h );
 				NV_ASSERT( i == index_type( m_storage->size() ), "Fail!" );
 				NV_UNUSED( i );
@@ -102,10 +108,10 @@
 			bool exists( handle_type h )
 			{
-				return m_index.exists( h );
+				return m_index->exists( h );
 			}
 
 			void call_destroy( value_type* data )
 			{
-				for ( auto dh : m_destroy )
+				for ( auto dh : this->m_destroy )
 					dh( data );
 			}
@@ -113,7 +119,7 @@
 			virtual void clear_() final
 			{
- 				for ( uint32 i = 0; i < m_storage->size(); ++i )
- 					call_destroy( &(*m_storage)[i] );
-				m_index.clear();
+				m_index->clear();
+				for ( uint32 i = 0; i < m_storage->size(); ++i )
+					call_destroy( &( *m_storage )[i] );
 				m_storage->clear();
 			}
@@ -121,10 +127,10 @@
 			value_type* get( handle_type h )
 			{
-				index_type i = m_index.get( h );
+				index_type i = m_index->get( h );
 				return i >= 0 ? &( ( *m_storage )[unsigned( i )] ) : nullptr;
 			}
 			const value_type* get( handle_type h ) const
 			{
-				index_type i = m_index.get( h );
+				index_type i = m_index->get( h );
 				return i >= 0 ? &( ( *m_storage )[unsigned( i )] ) : nullptr;
 			}
@@ -133,10 +139,10 @@
 			const void* get_raw( handle_type h ) const { return get( h ); }
 
-			virtual void remove( handle_type h ) final
+			virtual void remove_( handle_type h ) final
 			{
 				value_type* v = get( h );
 				if ( v == nullptr ) return;
 				call_destroy( v );
-				index_type dead_eindex = m_index.remove_swap( h );
+				index_type dead_eindex = m_index->remove_swap( h );
 				if ( dead_eindex == -1 ) return;
 				if ( dead_eindex != static_cast<index_type>( m_storage->size() - 1 ) )
@@ -149,9 +155,9 @@
 			}
 
-			void remove_by_index( index_type i )
-			{
-				if ( i > size() ) return;
+			void remove_by_index_( uint32 i )
+			{
+				if ( i > m_storage->size() ) return;
 				call_destroy( &(*m_storage)[i] );
-				index_type dead_eindex = m_index.remove_swap_by_index( i );
+				index_type dead_eindex = m_index->remove_swap_by_index( i );
 				if ( dead_eindex == -1 ) return;
 				if ( dead_eindex != static_cast<index_type>( m_storage->size() - 1 ) )
@@ -164,18 +170,4 @@
 			}
 
-			template < typename F >
-			void remove_if( F f )
-			{
-				uint32 i = 0;
-				while ( i < size() )
-				{
-					auto& md = ( *m_storage )[i];
-					if ( f( md ) )
-						remove_by_index( i );
-					else
-						++i;
-				}
-			}
-
 			virtual void on_attach( handle_type, handle_type child ) final
 			{
@@ -186,23 +178,12 @@
 			}
 
-			~component()
-			{
-				clear_();
-				delete m_storage;
-				m_storage = nullptr;
+			virtual ~component()
+			{
+				//clear_();
+				delete m_index;
 			}
 			
-			inline const value_type& operator[] ( index_type i ) const { return ( *m_storage )[i]; }
-			inline value_type& operator[] ( index_type i ) { return ( *m_storage )[i]; }
-
 			inline size_t size() const { return m_storage->size(); }
-
-			inline iterator        begin() { return m_storage->begin(); }
-			inline const_iterator  begin()  const { return m_storage->begin(); }
-
-			inline iterator        end() { return m_storage->end(); }
-			inline const_iterator  end()  const { return m_storage->end(); }
-
-			inline virtual component_storage* storage() { return m_storage; }
+ 			inline virtual component_storage* storage() { return m_storage; }
 		protected:
 			ecs_type&        m_ecs;
@@ -211,8 +192,8 @@
 			void swap( handle_type a, handle_type b )
 			{
-				index_type ai = m_index.get( a );
-				index_type bi = m_index.get( b );
+				index_type ai = m_index->get( a );
+				index_type bi = m_index->get( b );
 				if ( ai < 0 || bi < 0 ) return;
-				m_index.swap( a, b );
+				m_index->swap( a, b );
 				value_type t = nv::move( ( *m_storage )[ai] );
 				( *m_storage )[ai] = nv::move( ( *m_storage )[bi] );
@@ -225,11 +206,4 @@
 			}
 
-			void relational_remove( handle_type a )
-			{
-				index_type i = m_index.get( a );
-				remove( a );
-				relational_rebuild( i );
-			}
-
 			void relational_rebuild( index_type i )
 			{
@@ -237,5 +211,5 @@
 				handle_type p = m_ecs.get_parent( h );
 				if ( !p ) return;
-				if ( i < m_index.get( p ) )
+				if ( i < m_index->get( p ) )
 				{
 					swap( h, p );
@@ -248,5 +222,5 @@
 				handle_type p = m_ecs.get_parent( h );
 				if ( !p ) return;
-				if ( m_index.get( h ) < m_index.get( p ) )
+				if ( m_index->get( h ) < m_index->get( p ) )
 				{
 					swap( h, p );
@@ -256,6 +230,6 @@
 			}
 
-			bool             m_relational;
-			index_table_type m_index;
+			bool       m_relational;
+			idx_table* m_index;
 		};
 
Index: trunk/nv/ecs/ecs.hh
===================================================================
--- trunk/nv/ecs/ecs.hh	(revision 550)
+++ trunk/nv/ecs/ecs.hh	(revision 551)
@@ -39,11 +39,13 @@
 			typedef ecs< Handle, MessageList, Time >   this_type;
 			typedef Handle                             handle_type;
-			using base_type::time_type;
-			using base_type::message_type;
-			using base_type::message;
-
-			using update_handler  = function< void( time_type ) >;
-			using destroy_handler = function< void( void* ) >;
-			using create_handler  = function< void( handle_type, void*, const lua::stack_proxy& ) >;
+			typedef typename base_type::time_type      time_type;
+			using typename base_type::message_list;
+			using typename base_type::message_type;
+			using typename base_type::message;
+
+			using destructor_handler = function< void() >;
+			using update_handler     = function< void( time_type ) >;
+			using destroy_handler    = function< void( void* ) >;
+			using create_handler     = function< void( handle_type, void*, const lua::stack_proxy& ) >;
 
 			class component_interface
@@ -51,11 +53,12 @@
 			public:
 				virtual void clear_() = 0;
-				virtual void remove( handle_type h ) = 0;
+				virtual void remove_( handle_type h ) = 0;
 				virtual void on_attach( handle_type parent, handle_type child ) = 0;
 				virtual void* get_raw( handle_type h ) = 0;
 				virtual void* insert_raw( handle_type h ) = 0;
 				virtual const void* get_raw( handle_type h ) const = 0;
-				virtual component_storage* storage() = 0;
 				virtual uint32 temp_index( handle_type h ) const = 0;
+				virtual void remove_by_index_( uint32 i ) = 0;
+
 
 				void run_create( handle_type h, const lua::stack_proxy& p )
@@ -140,5 +143,5 @@
 			}
 
-			template < typename... Components >
+			template < int, typename... Components >
 			struct gather_components
 			{
@@ -175,10 +178,10 @@
 
 			private:
-				template < int Index, typename C, typename... Components >
+				template < int Index, typename C, typename... Cs >
 				void fill( this_type& ecs )
 				{
 					cis[Index] = ecs.get_interface< C >();
 					NV_ASSERT( cis[Index], "What the fuck is this?" );
-					fill< Index + 1, Components... >( ecs );
+					fill< Index + 1, Cs... >( ecs );
 				}
 
@@ -186,17 +189,17 @@
 				void fill( this_type& ) {}
 
-				template < int Index, typename SC, typename C, typename... Components >
+				template < int Index, typename SC, typename C, typename... Cs >
 				C& run_get()
 				{
-					return get_impl< Index, SC, C, Components... >( nv::is_same< SC, C >{} );
-				}
-
-				template < int Index, typename SC, typename C, typename C2, typename... Components >
+					return get_impl< Index, SC, C, Cs... >( nv::is_same< SC, C >{} );
+				}
+
+				template < int Index, typename SC, typename C, typename C2, typename... Cs >
 				C& get_impl( false_type&& )
 				{
-					return get_impl< Index + 1, SC, C2, Components...>( nv::is_same< SC, C >() );
-				}
-
-				template < int Index, typename SC, typename C, typename... Components >
+					return get_impl< Index + 1, SC, C2, Cs...>( nv::is_same< SC, C >() );
+				}
+
+				template < int Index, typename SC, typename C, typename... Cs >
 				C& get_impl( true_type&& )
 				{
@@ -206,8 +209,8 @@
 			};
 
-			template <>
-			struct gather_components<>
-			{
-				gather_components( this_type& ) {};
+			template <int I>
+			struct gather_components<I>
+			{
+				gather_components( this_type& ) {}
 				bool run( handle_type ) { return true;  }
 				template < typename Component >
@@ -215,11 +218,15 @@
 			};
 
-			template< typename System >
-			void register_system( string_view name, System* c )
-			{
-				register_handler< System >( name, (System*)( c ) );
-				register_component_helper< System >( c, has_components< System >::type{} );
-				register_ecs_update< System >( (System*)( c ),
-					has_ecs_update< this_type, System, time_type >::type{} );
+			template< typename System, typename... Args >
+			System* register_system( string_view name, Args&&... args )
+			{
+				System* result = new System( nv::forward< Args >( args )... );
+
+				this->template register_handler< System >( name, (System*)( result ) );
+				register_component_helper< System >( result, typename has_components< System >::type() );
+				register_ecs_update< System >( (System*)( result ),
+					typename has_ecs_update< this_type, System, time_type >::type() );
+				m_cleanup.emplace_back( [=] () { delete result; } );
+				return result;
 			}
 
@@ -227,10 +234,12 @@
 			void register_component_helper( System* c, true_type&& )
 			{
-				using component_list = System::components;
-				register_component_messages< System, component_list >( c, message_list{} );
-				register_ecs_component_update< System >( c, component_list{},
-					has_ecs_component_update< this_type, System, component_list, time_type >::type{} );
-				register_component_update< System >( c, component_list{},
-					has_component_update< System, component_list, time_type >::type{} );
+				using component_list = typename System::components;
+				register_component_messages< System, component_list >( c, message_list() );
+				register_ecs_component_update< System >( c, component_list(),
+					typename has_ecs_component_update< this_type, System, component_list, time_type >::type() );
+				register_component_update< System >( c, component_list(),
+					typename has_component_update< System, component_list, time_type >::type() );
+				register_destroy< System, mpl::head<component_list> >( (System*)( c ),
+					typename has_destroy< System, mpl::head<component_list> >::type() );
 			}
 
@@ -241,25 +250,36 @@
 
 			template < typename Component, typename Handler >
-			void register_component( string_view name, component_interface* c )
-			{
+			component_storage* register_component( string_view name, component_interface* c )
+			{
+				auto s = new component_storage_handler< Component >;
+				m_cstorage.push_back( s );
+				m_cmap[thash64::create<Component>()] = s;
+				m_cmap_by_name[name] = s;
+
 				m_components.push_back( c );
 				m_component_map[thash64::create<Component>()] = c;
 				m_component_map_by_name[name] = c;
-				register_handler< Handler >( name, (Handler*)c );
-				register_component_messages< Handler, mpl::list< Component > >( (Handler*)( c ), message_list{} );
-				register_ecs_component_update< Handler >( (Handler*)( c ), mpl::list< Component >{},
-					has_ecs_component_update< this_type, Handler, mpl::list< Component >, time_type >::type{} );
-				register_component_update< Handler >( (Handler*)( c ), mpl::list< Component >{}, 
-					has_component_update< Handler, mpl::list< Component >, time_type >::type{} );
+				this->template register_handler< Handler >( name, (Handler*)c );
+				register_component_messages< Handler, mpl::list< Component > >( (Handler*)( c ), message_list() );
+				register_ecs_component_update< Handler >( (Handler*)( c ), mpl::list< Component >(),
+					typename has_ecs_component_update< this_type, Handler, mpl::list< Component >, time_type >::type() );
+				register_component_update< Handler >( (Handler*)( c ), mpl::list< Component >(),
+					typename has_component_update< Handler, mpl::list< Component >, time_type >::type() );
 				register_ecs_update< Handler >( (Handler*)( c ), 
-					has_ecs_update< this_type, Handler, time_type >::type{} );
+					typename has_ecs_update< this_type, Handler, time_type >::type() );
 				register_destroy< Handler, Component >( (Handler*)( c ),
-					has_destroy< Handler, Component >::type{} );
+					typename has_destroy< Handler, Component >::type() );
 				register_create< Handler, Component, handle_type >( (Handler*)( c ),
-					has_create< Handler, Component, handle_type >::type{} );
-				//				auto s = c->storage();
-				// 				m_cstorage.push_back( s );
-				// 				m_cmap[thash64::create<Component>()] = s;
-				// 				m_cmap_by_name[name] = s;
+					typename has_create< Handler, Component, handle_type >::type() );
+				
+				return s;
+			}
+
+			template < typename ComponentManager, typename ...Args >
+			ComponentManager* register_component_manager( Args&&... args )
+			{
+				ComponentManager* result = new ComponentManager( *this, nv::forward< Args >( args )... );
+				m_cleanup.emplace_back( [=] () { delete result; } );
+				return result;
 			}
 
@@ -271,12 +291,15 @@
 			void update( time_type dtime )
 			{
-				update_time( dtime );
+				this->update_time( dtime );
 				for ( auto u : m_update_handlers )
 					u( dtime );
+				for ( auto h : m_dead_handles )
+					remove( h );
+				m_dead_handles.clear();
 			}
 
 			void clear()
 			{
-				reset_events();
+				this->reset_events();
 				for ( auto c : m_components )
 					c->clear_();
@@ -286,8 +309,15 @@
 			~ecs()
 			{
+				for ( auto s : m_cstorage )
+					delete s;
+				m_cstorage.clear();
 				// 				for ( auto cs : m_component_storage )
 				// 					delete cs;
 				//				m_cstorage.clear();
-
+				// reverse order delete;
+				if ( !m_cleanup.empty() )
+				for ( int i = (int)m_cleanup.size() - 1; i >= 0; --i )
+					m_cleanup[i]();
+				m_cleanup.clear();
 			}
 
@@ -343,4 +373,36 @@
 			}
 
+			template < typename C, typename F >
+			void remove_component_if( F&& f )
+			{
+				auto storage = get_storage<C>();
+				auto temp_component = get_interface<C>();
+				uint32 i = 0;
+				while ( i < storage->size() )
+					if ( f( ( *storage )[i] ) )
+						temp_component->remove_by_index_( i );
+					else
+						++i;
+			}
+
+			template < typename C>
+			void remove_component( handle_type h )
+			{
+				auto temp_component = get_interface<C>();
+				temp_component->remove_( h );
+			}
+
+			template < typename C, typename F >
+			void for_each( F&& f )
+			{
+				auto storage = get_storage<C>();
+				NV_ASSERT( storage, "Invalid component" );
+				for ( auto& c : *storage )
+				{
+					f( c );
+				}
+			}
+
+
 			template < typename F >
 			void recursive_call( handle_type h, F&& f )
@@ -351,4 +413,9 @@
 			}
 
+			void mark_remove( handle_type h )
+			{
+				m_dead_handles.push_back( h );
+			}
+
 			void remove( handle_type h )
 			{
@@ -360,5 +427,5 @@
 				}
 				for ( auto c : m_components )
-					c->remove( h );
+					c->remove_( h );
 				m_handles.free_handle( h );
 			}
@@ -396,5 +463,7 @@
 			Component* get( handle_type h )
 			{
-				return static_cast<Component*>( m_component_map[thash64::create< Component >()]->get_raw( h ) );
+				auto it = m_component_map.find( thash64::create< Component >() );
+				NV_ASSERT( it != m_component_map.end(), "Get fail!" );
+				return static_cast<Component*>( it->second->get_raw( h ) );
 			}
 
@@ -402,9 +471,11 @@
 			const Component* get( handle_type h ) const
 			{
-				return static_cast<const Component*>( m_component_map[thash64::create< Component >()]->get_raw( h ) );
-			}
-
-			template < typename Component >
-			typename component_storage_handler< Component >*
+				auto it = m_component_map.find( thash64::create< Component >() );
+				NV_ASSERT( it != m_component_map.end(), "Get fail!" );
+				return static_cast<const Component*>( it->second->get_raw( h ) );
+			}
+
+			template < typename Component >
+			component_storage_handler< Component >*
 				get_storage()
 			{
@@ -413,8 +484,24 @@
 
 			template < typename Component >
-			const typename component_storage_handler< Component >*
+			const component_storage_handler< Component >*
 				get_storage() const
 			{
 				return storage_cast<Component>( m_cmap[thash64::create< Component >()] );
+			}
+			
+			template < typename Component >
+			Component& add_component( handle_type h )
+			{
+				component_interface* ci = get_interface<Component>();
+				return *((Component*)ci->insert_raw( h ));
+			}
+
+			template < typename Component, typename ...Args >
+			Component& add_component( handle_type h, Args&&... args )
+			{
+				component_interface* ci = get_interface<Component>();
+				Component& result = *( (Component*)ci->insert_raw( h ) );
+				result = Component{ nv::forward<Args>( args )... };
+				return result;
 			}
 
@@ -422,9 +509,9 @@
 			void register_component_messages( System* h, List<Messages...>&& )
 			{
-				int unused_0[] = { ( register_ecs_component_message<System,Messages>( h, Components{} , has_ecs_component_message< this_type, System, Components, Messages >::type{} ), 0 )... };
-				int unused_1[] = { ( register_component_message<System,Messages>( h, Components{}, has_component_message< System, Components, Messages >::type{} ), 0 )... };
-				int unused_2[] = { ( register_ecs_message<System,Messages>( h, has_ecs_message< this_type, System, Messages >::type{} ), 0 )... };
-			}
-
+				int unused_0[] = { ( register_ecs_component_message<System,Messages>( h, Components() , typename has_ecs_component_message< this_type, System, Components, Messages >::type() ), 0 )... };
+				int unused_1[] = { ( register_component_message<System,Messages>( h, Components(), typename has_component_message< System, Components, Messages >::type() ), 0 )... };
+				int unused_2[] = { ( register_ecs_message<System,Messages>( h, typename has_ecs_message< this_type, System, Messages >::type() ), 0 )... };
+			}
+			
 			template < typename System, typename Message, typename C, typename... Cs >
 			void register_component_message( System* s, mpl::list< C, Cs...>&&, true_type&& )
@@ -438,7 +525,7 @@
 						if ( void* c = ci->get_raw( h ) )
 						{
-							gather_components< Cs... > gather( *this );
+							gather_components<0, Cs... > gather( *this );
 							if ( gather.run( h ) )
-								s->on( m, *( (C*)( c ) ), gather.get<Cs>()... );
+								s->on( m, *( (C*)( c ) ), gather.template get<Cs>()... );
 						}
 					};
@@ -462,7 +549,7 @@
 						if ( void* c = ci->get_raw( h ) )
 						{
-							gather_components< Cs... > gather( *this );
+							gather_components<0, Cs... > gather( *this );
 							if ( gather.run( h ) )
-								s->on( m, *this, *( (C*)( c ) ), gather.get<Cs>()... );
+								s->on( m, *this, *( (C*)( c ) ), gather.template get<Cs>()... );
 						}
 					};
@@ -514,10 +601,10 @@
 			}
 
-			template < typename System, typename Component, typename Handle >
+			template < typename System, typename Component, typename H >
 			void register_create( System* s, true_type&& )
 			{
 				component_interface* ci = get_interface<Component>();
 				NV_ASSERT( ci, "Unregistered component!" );
-				ci->m_create.push_back( [=] ( Handle h, void* data, const lua::stack_proxy& p )
+				ci->m_create.push_back( [=] ( H h, void* data, const lua::stack_proxy& p )
 				{
 					s->create( h, *( (Component*)data ), p );
@@ -529,13 +616,12 @@
 			void register_component_update( System* s, mpl::list< C, Cs...>&&, true_type&& )
 			{
-				component_interface* ci = get_interface<C>();
-				component_storage_handler<C>* storage = storage_cast<C>( ci->storage() );
+				component_storage_handler<C>* storage = get_storage<C>();
 				register_update( [=] ( time_type dtime )
 				{
-					gather_components< Cs... > gather( *this );
+					gather_components<0, Cs... > gather( *this );
 					for ( auto& c : *storage )
 					{
 						if ( gather.runc( c ) )
-							s->update( c, gather.get<Cs>()..., dtime );
+							s->update( c, gather.template get<Cs>()..., dtime );
 					}
 				} );
@@ -545,13 +631,12 @@
 			void register_ecs_component_update( System* s, mpl::list< C, Cs...>&&, true_type&& )
 			{
-				component_interface* ci = get_interface<C>();
-				component_storage_handler<C>* storage = storage_cast<C>( ci->storage() );
+				component_storage_handler<C>* storage = get_storage<C>();
 				register_update( [=] ( time_type dtime )
 				{
-					gather_components< Cs... > gather(*this);
+					gather_components<0, Cs... > gather(*this);
 					for ( auto& c : *storage )
 					{
 						if ( gather.runc( c ) )
-							s->update( *this, c, gather.get<Cs>()..., dtime );
+							s->update( *this, c, gather.template get<Cs>()..., dtime );
 					}
 				} );
@@ -565,5 +650,5 @@
 			void register_destroy( System*, false_type&& ) {}
 
-			template < typename System, typename Component, typename Handle >
+			template < typename System, typename Component, typename H >
 			void register_create( System*, false_type&& ) {}
 
@@ -582,4 +667,5 @@
 
 			handle_tree_manager< handle_type >          m_handles;
+			vector< handle_type >                       m_dead_handles;
 			vector< component_interface* >              m_components;
 			hash_store< thash64, component_interface* > m_component_map;
@@ -587,7 +673,10 @@
 			vector< update_handler >                    m_update_handlers;
 
-			// 			vector< component_storage* >                m_cstorage;
-			// 			hash_store< thash64, component_storage* >   m_cmap;
-			// 			hash_store< shash64, component_storage* >   m_cmap_by_name;
+			vector< component_storage* >                m_cstorage;
+			hash_store< thash64, component_storage* >   m_cmap;
+			hash_store< shash64, component_storage* >   m_cmap_by_name;
+
+			vector< component_interface* >              m_temp_components;
+			vector< destructor_handler >                m_cleanup;
 		};
 
Index: trunk/nv/ecs/message_queue.hh
===================================================================
--- trunk/nv/ecs/message_queue.hh	(revision 550)
+++ trunk/nv/ecs/message_queue.hh	(revision 551)
@@ -84,6 +84,5 @@
 			}
 
-			template<>
-			void register_handler<void>( string_view /*name*/, void* )
+			void register_handler( string_view /*name*/, void* )
 			{
 			}
@@ -123,4 +122,13 @@
 				message m{ Payload::message_id, 0, m_time + delay };
 				new( &m.payload ) Payload{ nv::forward<Args>( args )... };
+				return queue( m );
+			}
+
+			// FIXME: Clang hack
+			template < typename Payload >
+			bool queue( time_type delay, Payload&& arg )
+			{
+				message m{ Payload::message_id, 0, m_time + delay };
+				new( &m.payload ) Payload( arg );
 				return queue( m );
 			}
Index: trunk/nv/engine/mesh_manager.hh
===================================================================
--- trunk/nv/engine/mesh_manager.hh	(revision 550)
+++ trunk/nv/engine/mesh_manager.hh	(revision 551)
@@ -79,5 +79,5 @@
 		string_view resolve( shash64 h ) { return ( *m_strings )[h]; }
 		resource< data_channel_set > get_path( const string_view& path,
-			resource< mesh_data > default = resource< mesh_data >(),
+			resource< mesh_data > def = resource< mesh_data >(),
 			data_node_info* info = nullptr );
 
Index: trunk/nv/engine/resource_system.hh
===================================================================
--- trunk/nv/engine/resource_system.hh	(revision 550)
+++ trunk/nv/engine/resource_system.hh	(revision 551)
@@ -194,5 +194,5 @@
 		virtual resource< T > load_resource( source_type u )
 		{
-			if ( exists( u.id() ) ) return this->template create< T >( u.id() );
+			if ( this->exists( u.id() ) ) return this->template create< T >( u.id() );
 			return create_resource( u );
 		}
Index: trunk/nv/interface/context.hh
===================================================================
--- trunk/nv/interface/context.hh	(revision 550)
+++ trunk/nv/interface/context.hh	(revision 551)
@@ -272,5 +272,5 @@
 		vertex_array create_vertex_array( const VTX* v, uint32 vcount, const IDX* i, uint32 icount, buffer_hint hint )
 		{
-			buffer       vb = this->create_buffer( VERTEX_BUFFER, hint, count * sizeof( VTX ), v );
+			buffer       vb = this->create_buffer( VERTEX_BUFFER, hint, vcount * sizeof( VTX ), v );
 			buffer       ib = this->create_buffer( INDEX_BUFFER, hint, icount * sizeof( IDX ), i );
 			vertex_array_desc desc;
Index: trunk/nv/interface/physics_world.hh
===================================================================
--- trunk/nv/interface/physics_world.hh	(revision 550)
+++ trunk/nv/interface/physics_world.hh	(revision 551)
@@ -24,5 +24,5 @@
 	{
 	public:
-		explicit collision_shape( void* p = nullptr, void* m = nullptr ) : internal( p ), mesh( m ) {}
+		collision_shape( void* p = nullptr, void* m = nullptr ) : internal( p ), mesh( m ) {}
 		void* internal = nullptr;
 		void* mesh     = nullptr;
@@ -33,5 +33,5 @@
 	{
 	public:
-		explicit rigid_body( void* p = nullptr ) : internal( p ) {}
+		rigid_body( void* p = nullptr ) : internal( p ) {}
 		void* internal = nullptr;
 	};
@@ -40,5 +40,5 @@
 	{
 	public:
-		explicit constraint( void* p = nullptr ) : internal( p ) {}
+		constraint( void* p = nullptr ) : internal( p ) {}
 		void* internal = nullptr;
 	};
Index: trunk/nv/lua/lua_handle.hh
===================================================================
--- trunk/nv/lua/lua_handle.hh	(revision 550)
+++ trunk/nv/lua/lua_handle.hh	(revision 551)
@@ -10,4 +10,5 @@
 #include <nv/common.hh>
 #include <nv/stl/handle.hh>
+#include <nv/stl/handle_manager.hh>
 #include <nv/lua/lua_values.hh>
 
Index: trunk/nv/lua/lua_proxy.hh
===================================================================
--- trunk/nv/lua/lua_proxy.hh	(revision 550)
+++ trunk/nv/lua/lua_proxy.hh	(revision 551)
@@ -12,4 +12,5 @@
 #include <nv/stl/string.hh>
 #include <nv/stl/string_table.hh>
+#include <nv/lua/lua_values.hh>
 
 namespace nv
@@ -87,6 +88,5 @@
 			bool read( T& t ) const
 			{
-				NV_ASSERT( m_state->get_type_data(), "stack_proxy::read - type database not created!" );
-				const type_database* tdb = m_state->get_type_data()->get_type_database();
+				const type_database* tdb = get_type_db();
 				NV_ASSERT( tdb, "stack_proxy::read - type database not set!" );
 				const type_entry* entry = tdb->get_type<T>();
@@ -100,4 +100,6 @@
 //			string_view to_string_view();
 		protected:
+			const type_database* get_type_db() const;
+
 			// Not public because not permanent
 			string_view get_string_view() const;
Index: trunk/nv/lua/lua_state.hh
===================================================================
--- trunk/nv/lua/lua_state.hh	(revision 550)
+++ trunk/nv/lua/lua_state.hh	(revision 551)
@@ -57,4 +57,5 @@
 			}
 			const type_database* get_type_database() const { return m_type_database; }
+			type_database* get_type_database() { return m_type_database; }
 
 			template < typename T >
@@ -204,7 +205,5 @@
 
 			const type_data* get_type_data() const { return m_lua_types; }
-
-
-
+			type_data* get_type_data() { return m_lua_types; }
 
 		protected:
Index: trunk/nv/stl/flags.hh
===================================================================
--- trunk/nv/stl/flags.hh	(revision 550)
+++ trunk/nv/stl/flags.hh	(revision 551)
@@ -227,9 +227,9 @@
 		}
 
-		template < uint32 SIZE, typename T >
-		inline friend flags< SIZE, T > operator &( const flags< SIZE, T >& lhs, const flags< SIZE, T >& rhs );
-
-		template < uint32 SIZE, typename T >
-		inline friend flags< SIZE, T > operator |( const flags< SIZE, T >& lhs, const flags< SIZE, T >& rhs );
+		template < uint32 S, typename T2 >
+		inline friend flags< S, T2 > operator &( const flags< S, T2 >& lhs, const flags< S, T2 >& rhs );
+
+		template < uint32 S, typename T2 >
+		inline friend flags< S, T2 > operator |( const flags< S, T2 >& lhs, const flags< S, T2 >& rhs );
 
 	private:
Index: trunk/nv/stl/functional/hash.hh
===================================================================
--- trunk/nv/stl/functional/hash.hh	(revision 550)
+++ trunk/nv/stl/functional/hash.hh	(revision 551)
@@ -86,8 +86,18 @@
 	};
 
+// 	template < typename T >
+// 	struct hash< T*, uintptr_t > : detail::hash_base< T, uintptr_t >
+// 	{
+// 		inline uintptr_t operator()( T* value ) const { return reinterpret_cast<uintptr_t>( value ); }
+// 		static constexpr uintptr_t get( T value ) { return reinterpret_cast<uintptr_t>( value ); }
+// 	};
+
 	template < typename T >
-	struct hash< T*, uintptr_t > : detail::hash_base< T, uintptr_t >
-	{
-		inline uintptr_t operator()( T* value ) const { return reinterpret_cast<uintptr_t>( value ); }
+		struct hash< T, uintptr_t, 
+		typename enable_if< is_function_pointer< T >::value,
+			void
+		>::type > : detail::hash_base< T, uintptr_t >
+	{
+		inline uintptr_t operator()( T value ) const { return reinterpret_cast<uintptr_t>( value ); }
 		static constexpr uintptr_t get( T value ) { return reinterpret_cast<uintptr_t>( value ); }
 	};
Index: trunk/nv/stl/handle_manager.hh
===================================================================
--- trunk/nv/stl/handle_manager.hh	(revision 550)
+++ trunk/nv/stl/handle_manager.hh	(revision 551)
@@ -25,5 +25,5 @@
 	{
 	public:
-		typedef typename Handle                  handle_type;
+		typedef Handle                           handle_type;
 		typedef typename handle_type::value_type value_type;
 
Index: trunk/nv/stl/handle_store.hh
===================================================================
--- trunk/nv/stl/handle_store.hh	(revision 550)
+++ trunk/nv/stl/handle_store.hh	(revision 551)
@@ -39,5 +39,5 @@
 		{
 			m_data.reserve( reserve );
-			m_indexes.reserve( reserve );
+			m_index.reserve( reserve );
 		}
 
@@ -98,5 +98,5 @@
 	private:
 		storage_type                  m_data;
-		index_table< handle_type, index_type > m_index;
+		flat_index_table< handle_type, index_type > m_index;
 	};
 
Index: trunk/nv/stl/index_table.hh
===================================================================
--- trunk/nv/stl/index_table.hh	(revision 550)
+++ trunk/nv/stl/index_table.hh	(revision 551)
@@ -31,6 +31,27 @@
 		typedef Index               index_type;
 
-		index_table() {}
-		index_table( uint32 reserve )
+		virtual index_type insert( handle_type h ) = 0;
+		virtual bool exists( handle_type h ) const = 0;
+		virtual index_type get( handle_type h ) const = 0;
+		virtual void swap( handle_type a, handle_type b ) = 0;
+		virtual index_type remove_swap( handle_type h ) = 0;
+		virtual index_type remove_swap_by_index( index_type dead_eindex ) = 0;
+		virtual void clear() = 0;
+		virtual uint32 size() const = 0;
+	};
+
+
+	template <
+		typename Handle,
+		typename Index = sint32
+	>
+	class flat_index_table : public index_table< Handle, Index >
+	{
+	public:
+		typedef Handle              handle_type;
+		typedef Index               index_type;
+
+		flat_index_table() {}
+		flat_index_table( uint32 reserve )
 		{
 			m_indexes.reserve( reserve );
@@ -129,5 +150,5 @@
 		typename Index = sint32
 	>
-		class hashed_index_table
+		class hashed_index_table : public index_table< Handle, Index >
 	{
 	public:
Index: trunk/nv/stl/math/geometric.hh
===================================================================
--- trunk/nv/stl/math/geometric.hh	(revision 550)
+++ trunk/nv/stl/math/geometric.hh	(revision 551)
@@ -69,5 +69,5 @@
 		inline T inv_length( T v )
 		{
-			return T( 1 ) / abs( x );
+			return T( 1 ) / abs( v );
 		}
 
Index: trunk/nv/stl/range.hh
===================================================================
--- trunk/nv/stl/range.hh	(revision 550)
+++ trunk/nv/stl/range.hh	(revision 551)
@@ -85,5 +85,5 @@
 			static const T invalid = T( 0 );
 
-			bits_iterator_base( T value, T current ) : forward_iterator_base<T>( current ), m_full( value )
+			bits_iterator_base( T value, T current ) : value_enumerator_base<T>( current ), m_full( value )
 			{
 				if( !( static_cast<base_type>( value ) & static_cast<base_type>( current ) ) )
Index: trunk/nv/stl/type_traits/common.hh
===================================================================
--- trunk/nv/stl/type_traits/common.hh	(revision 550)
+++ trunk/nv/stl/type_traits/common.hh	(revision 551)
@@ -29,7 +29,4 @@
 	};
 
-	template < typename T >
-	constexpr T integral_constant_v = integral_constant<T>::value;
-
 	// TODO: Propagate
 	template< bool B >
@@ -38,7 +35,4 @@
 	typedef bool_constant<true> true_type;
 	typedef bool_constant<false> false_type;
-
-	template < typename T >
-	constexpr T bool_constant_v = bool_constant<T>::value;
 
 	template< bool Test, typename T = void>
Index: trunk/nv_bullet.lua
===================================================================
--- trunk/nv_bullet.lua	(revision 550)
+++ trunk/nv_bullet.lua	(revision 551)
@@ -28,25 +28,25 @@
 
 	includedirs { "D:/Libraries/bullet2/src/" }
-
-	configuration "debug"
+	
+	filter { "configurations:debug", "platforms:*32" }
 		nv_bullet_configure( "D:/Libraries/bullet2/x86/", "RelWithDebInfo", "_rdbg" )
 --		nv_bullet_configure( "D:/Libraries/bullet2/x86/", "Debug", "_debug" )
 
-	configuration "profiler"
+	filter { "configurations:profiler", "platforms:*32" }
 		nv_bullet_configure( "D:/Libraries/bullet2/x86/", "Release" )
 
-	configuration "release"
+	filter { "configurations:release", "platforms:*32" }
 		nv_bullet_configure( "D:/Libraries/bullet2/x86/", "Release" )
 
-	configuration "debug_64"
+	filter { "configurations:debug", "platforms:*64" }
 		nv_bullet_configure( "D:/Libraries/bullet2/x86_64/", "RelWithDebInfo", "_rdbg" )
 --		nv_bullet_configure( "D:/Libraries/bullet2/x86_64/", "Debug", "_debug" )
 
-	configuration "profiler_64"
+	filter { "configurations:profiler", "platforms:*64" }
 		nv_bullet_configure( "D:/Libraries/bullet2/x86_64/", "Release" )
 
-	configuration "release_64"
+	filter { "configurations:release", "platforms:*64" }
 		nv_bullet_configure( "D:/Libraries/bullet2/x86_64/", "Release" )
 
+	filter {}
 
-
Index: trunk/premake5.lua
===================================================================
--- trunk/premake5.lua	(revision 550)
+++ trunk/premake5.lua	(revision 551)
@@ -1,3 +1,3 @@
-solution "nv"
+workspace "nv"
 	configurations { "debug", "release" }
 	targetdir "bin"
Index: trunk/src/engine/mesh_manager.cc
===================================================================
--- trunk/src/engine/mesh_manager.cc	(revision 550)
+++ trunk/src/engine/mesh_manager.cc	(revision 551)
@@ -29,7 +29,7 @@
 }
 
-nv::resource< nv::data_channel_set > nv::mesh_data_manager::get_path( const string_view& path, resource< mesh_data > default /*= resource< mesh_data >()*/, data_node_info* info /*= nullptr */ )
+nv::resource< nv::data_channel_set > nv::mesh_data_manager::get_path( const string_view& path, resource< mesh_data > def /*= resource< mesh_data >()*/, data_node_info* info /*= nullptr */ )
 {
-	nv::resource< nv::mesh_data > mr = default;
+	nv::resource< nv::mesh_data > mr = def;
 
 	nv::string_view sub_mesh_name;
Index: trunk/src/image/png_writer.cc
===================================================================
--- trunk/src/image/png_writer.cc	(revision 550)
+++ trunk/src/image/png_writer.cc	(revision 551)
@@ -12,5 +12,5 @@
 #include <stdarg.h>  
 
-using namespace nv;
+//using namespace nv;
 
 #if NV_COMPILER == NV_CLANG
@@ -33,8 +33,8 @@
 };
 
-#define STBI_MALLOC(sz)     nvmalloc(sz)
-#define STBI_REALLOC(p,sz)  nvrealloc(p,sz)
-#define STBI_FREE(p)        nvfree(p)
-#define STBI_MEMMOVE(d,s,c) nvmemmove(d,s,c)
+#define STBI_MALLOC(sz)     nv::nvmalloc(sz)
+#define STBI_REALLOC(p,sz)  nv::nvrealloc(p,sz)
+#define STBI_FREE(p)        nv::nvfree(p)
+#define STBI_MEMMOVE(d,s,c) nv::nvmemmove(d,s,c)
 
 typedef void stbi_write_func( void *context, void *data, int size );
@@ -57,7 +57,7 @@
 
 template < typename T >
-inline uchar8 byte_cast( T x )
-{
-	return uchar8( ( x ) & 255 );
+inline nv::uchar8 byte_cast( T x )
+{
+	return nv::uchar8( ( x ) & 255 );
 }
 
@@ -246,7 +246,12 @@
 }
 
+static int iabs( int i )
+{
+	return i >= 0 ? i : -i;
+}
+
 static unsigned char stbiw__paeth( int a, int b, int c )
 {
-	int p = a + b - c, pa = abs( p - a ), pb = abs( p - b ), pc = abs( p - c );
+	int p = a + b - c, pa = iabs( p - a ), pb = iabs( p - b ), pc = iabs( p - c );
 	if ( pa <= pb && pa <= pc ) return byte_cast( a );
 	if ( pb <= pc ) return byte_cast( b );
@@ -320,5 +325,5 @@
 				if ( p ) break;
 				for ( i = 0; i < x*n; ++i )
-					est += abs( (signed char)line_buffer[i] );
+					est += iabs( (signed char)line_buffer[i] );
 				if ( est < bestval ) { bestval = est; best = k; }
 			}
Index: trunk/src/lua/lua_proxy.cc
===================================================================
--- trunk/src/lua/lua_proxy.cc	(revision 550)
+++ trunk/src/lua/lua_proxy.cc	(revision 551)
@@ -76,4 +76,10 @@
 }
 
+const nv::type_database* nv::lua::stack_proxy::get_type_db() const
+{
+	NV_ASSERT( m_state->get_type_data(), "type database not created!" );
+	return m_state->get_type_data()->get_type_database();
+}
+
 nv::string_view nv::lua::stack_proxy::get_string_view() const
 {
