Index: trunk/nv/lua/lua_map_area.hh
===================================================================
--- trunk/nv/lua/lua_map_area.hh	(revision 264)
+++ trunk/nv/lua/lua_map_area.hh	(revision 265)
@@ -22,5 +22,5 @@
 		void register_map_area( lua_State* L );
 		void register_map_area_interface( lua_State* L, int index );
-		void register_map_area_instance( lua_State* L, int object_index, map_area* area );
+		void register_map_area_instance( lua_State* L, ref object_index, map_area* area );
 
 		namespace detail
Index: trunk/nv/lua/lua_object.hh
===================================================================
--- trunk/nv/lua/lua_object.hh	(revision 264)
+++ trunk/nv/lua/lua_object.hh	(revision 265)
@@ -27,8 +27,4 @@
 			};
 
-			void  push_object  ( lua_State *L, object* o );
-			void* to_object  ( lua_State *L, int index );
-			void* to_object  ( lua_State *L, int index, void* def );
-
 		}
 
@@ -36,5 +32,11 @@
 		struct pass_traits<object*>
 		{
-			static void push( lua_State *L, object* o ) { detail::push_object( L, o ); }
+			static void push( lua_State *L, object* o ) 
+			{
+				if ( o )
+					detail::push_ref_object( L, lua::ref(o->get_lua_index()) );
+				else
+					detail::push_nil( L );
+			}
 			static object* to( lua_State *L, int index ) { return static_cast< object* >( detail::to_ref_object( L, index ) ); }
 			static object* to( lua_State *L, int index, object* def ) { return static_cast< object* >( detail::to_ref_object( L, index, def ) ); }
Index: trunk/nv/lua/lua_state.hh
===================================================================
--- trunk/nv/lua/lua_state.hh	(revision 264)
+++ trunk/nv/lua/lua_state.hh	(revision 265)
@@ -26,9 +26,6 @@
 	namespace lua
 	{
-		const int ref_none  = -2;
-		const int ref_nil   = -1;
+
 		const int ret_multi = -1;
-
-		typedef int reference;
 
 		class state_wrapper
@@ -196,8 +193,8 @@
 			void log_stack();
 			lua_State* get_raw();
-			reference register_object( void* o, const char* lua_name );
-			reference register_proto( const char* id, const char* storage );
-			void store_metadata( reference object_index, const std::string& metaname, void* pointer );
-			void unregister_object( reference object_index );
+			ref register_object( void* o, const char* lua_name );
+			ref register_proto( const char* id, const char* storage );
+			void store_metadata( ref object_index, const std::string& metaname, void* pointer );
+			void unregister_object( ref object_index );
 
 			void register_enum( const char* name, int value );
Index: trunk/nv/lua/lua_values.hh
===================================================================
--- trunk/nv/lua/lua_values.hh	(revision 264)
+++ trunk/nv/lua/lua_values.hh	(revision 265)
@@ -18,4 +18,18 @@
 	namespace lua
 	{
+		class ref
+		{
+		public:
+			static const int none  = -2;
+			static const int nil   = -1;
+
+			ref() : m_value( nil ) {}
+			explicit ref( int lua_ref ) : m_value( lua_ref ) {}
+			bool is_valid() const { return m_value >= 0; }
+			int get() const { return m_value; }
+		protected:
+			int m_value;
+		};
+
 		typedef ptrdiff_t     linteger;
 		typedef unsigned long lunsigned;
@@ -54,11 +68,13 @@
 			int upvalue_index( int i );
 
-			void push_unsigned( lua_State *L, lunsigned v );
-			void push_integer ( lua_State *L, linteger v );
-			void push_number  ( lua_State *L, lnumber v );
-			void push_bool    ( lua_State *L, bool v );
-			void push_string  ( lua_State *L, const std::string& s );
-			void push_cstring ( lua_State *L, const char* s );
-			void push_pointer ( lua_State *L, void* p );
+			void push_nil        ( lua_State *L );
+			void push_unsigned   ( lua_State *L, lunsigned v );
+			void push_integer    ( lua_State *L, linteger v );
+			void push_number     ( lua_State *L, lnumber v );
+			void push_bool       ( lua_State *L, bool v );
+			void push_string     ( lua_State *L, const std::string& s );
+			void push_cstring    ( lua_State *L, const char* s );
+			void push_pointer    ( lua_State *L, void* p );
+			void push_ref_object ( lua_State *L, ref object );
 
 			lunsigned   to_unsigned   ( lua_State *L, int index );
@@ -137,4 +153,10 @@
 			static std::string to( lua_State *L, int index ) { return detail::to_string( L, index ); }
 			static std::string to( lua_State *L, int index, const std::string& def ) { return detail::to_string( L, index, def ); }
+		};
+
+		template <>
+		struct pass_traits<ref>
+		{
+			static void push( lua_State *L, ref s ) { detail::push_ref_object( L, s ); }
 		};
 
Index: trunk/nv/object.hh
===================================================================
--- trunk/nv/object.hh	(revision 264)
+++ trunk/nv/object.hh	(revision 265)
@@ -165,12 +165,12 @@
 
 	protected:
-		string  m_id;              ///< id type of the object
-		string  m_name;            ///< name of the object
-		uid     m_uid;             ///< uid of the object
-		int     m_lua_index;       ///< lua reference
-		int     m_lua_proto_index; ///< lua reference
-		object* m_parent;          ///< pointer to parent
-		list    m_children;        ///< children objects
-		size_t  m_child_count;     ///< number of children
+		string   m_id;              ///< id type of the object
+		string   m_name;            ///< name of the object
+		uid      m_uid;             ///< uid of the object
+		int      m_lua_index;       ///< lua reference
+		int      m_lua_proto_index; ///< lua reference
+		object*  m_parent;          ///< pointer to parent
+		list     m_children;        ///< children objects
+		size_t   m_child_count;     ///< number of children
 	};
 
Index: trunk/src/lua/lua_map_area.cc
===================================================================
--- trunk/src/lua/lua_map_area.cc	(revision 264)
+++ trunk/src/lua/lua_map_area.cc	(revision 265)
@@ -233,7 +233,7 @@
 }
 
-void nv::lua::register_map_area_instance( lua_State* L, int object_index, map_area* area )
-{
-	lua_rawgeti( L, LUA_REGISTRYINDEX, object_index );
+void nv::lua::register_map_area_instance( lua_State* L, ref object_index, map_area* area )
+{
+	lua_rawgeti( L, LUA_REGISTRYINDEX, object_index.get() );
 	lua_pushstring( L, "__map_area_ptr" );
 	lua_pushlightuserdata( L, (map_area*)area );
Index: trunk/src/lua/lua_object.cc
===================================================================
--- trunk/src/lua/lua_object.cc	(revision 264)
+++ 	(revision )
@@ -1,21 +1,0 @@
-// Copyright (C) 2012-2013 ChaosForge / Kornel Kisielewicz
-// http://chaosforge.org/
-//
-// This file is part of NV Libraries.
-// For conditions of distribution and use, see copyright notice in nv.hh
-
-#include "nv/lua/lua_object.hh"
-
-#include "nv/lua/lua_raw.hh"
-
-void nv::lua::detail::push_object  ( lua_State *L, object* o )
-{
-	if ( o == nullptr )
-	{
-		lua_pushnil( L );
-	}
-	else
-	{
-		lua_rawgeti( L, LUA_REGISTRYINDEX, o->get_lua_index() );
-	}
-}
Index: trunk/src/lua/lua_state.cc
===================================================================
--- trunk/src/lua/lua_state.cc	(revision 264)
+++ trunk/src/lua/lua_state.cc	(revision 265)
@@ -328,7 +328,7 @@
 }
 
-lua::reference lua::state::register_object( void* o, const char* lua_name )
-{
-	if ( o == nullptr ) return ref_none;
+lua::ref lua::state::register_object( void* o, const char* lua_name )
+{
+	if ( o == nullptr ) return lua::ref( lua::ref::none );
 	stack_guard guard( this );
 	lua_getglobal( m_state, lua_name );
@@ -338,8 +338,8 @@
 	}
 	deep_pointer_copy( -1, o );
-	return luaL_ref( m_state, LUA_REGISTRYINDEX );
-}
-
-lua::reference lua::state::register_proto( const char* id, const char* storage )
+	return lua::ref( luaL_ref( m_state, LUA_REGISTRYINDEX ) );
+}
+
+lua::ref lua::state::register_proto( const char* id, const char* storage )
 {
 	stack_guard guard( this );
@@ -354,5 +354,5 @@
 		NV_THROW( runtime_error, std::string( id ) + " not found in " + std::string( storage ) + " storage!" );
 	}
-	return luaL_ref( m_state, LUA_REGISTRYINDEX );
+	return lua::ref( luaL_ref( m_state, LUA_REGISTRYINDEX ) );
 }
 
@@ -369,14 +369,14 @@
 }
 
-void lua::state::unregister_object( reference object_index )
-{
-	if ( object_index == ref_nil ) return;
+void lua::state::unregister_object( ref object_index )
+{
+	if ( !object_index.is_valid() ) return;
 	stack_guard guard( this );
-	lua_rawgeti( m_state, LUA_REGISTRYINDEX, object_index );
+	lua_rawgeti( m_state, LUA_REGISTRYINDEX, object_index.get() );
 	lua_pushstring( m_state, "__ptr" );
 	lua_pushboolean( m_state, false );
 	lua_rawset( m_state, -3 );
 	lua_pop( m_state, 1 );
-	luaL_unref( m_state, LUA_REGISTRYINDEX, object_index );
+	luaL_unref( m_state, LUA_REGISTRYINDEX, object_index.get() );
 }
 
@@ -419,8 +419,8 @@
 }
 
-void nv::lua::state::store_metadata( reference object_index, const std::string& metaname, void* pointer )
-{
-	if ( object_index == ref_nil ) return;
-	lua_rawgeti( m_state, LUA_REGISTRYINDEX, object_index );
+void nv::lua::state::store_metadata( ref object_index, const std::string& metaname, void* pointer )
+{
+	if ( !object_index.is_valid() ) return;
+	lua_rawgeti( m_state, LUA_REGISTRYINDEX, object_index.get() );
 	lua_pushstring( m_state, metaname.c_str() );
 	lua_pushlightuserdata( m_state, pointer );
Index: trunk/src/lua/lua_values.cc
===================================================================
--- trunk/src/lua/lua_values.cc	(revision 264)
+++ trunk/src/lua/lua_values.cc	(revision 265)
@@ -40,4 +40,9 @@
 }
 
+void nv::lua::detail::push_nil( lua_State *L )
+{
+	lua_pushnil( L );
+}
+
 void nv::lua::detail::push_unsigned( lua_State *L, lunsigned v )
 {
@@ -74,4 +79,10 @@
 	lua_pushlightuserdata( L, p );
 }
+
+void nv::lua::detail::push_ref_object ( lua_State *L, ref object )
+{
+	lua_rawgeti( L, LUA_REGISTRYINDEX, object.get() );
+}
+
 
 lunsigned   nv::lua::detail::to_unsigned( lua_State *L, int index )
Index: trunk/src/object.cc
===================================================================
--- trunk/src/object.cc	(revision 264)
+++ trunk/src/object.cc	(revision 265)
@@ -18,6 +18,6 @@
 	, m_name()
 	, m_uid(0)
-	, m_lua_index(lua::ref_none)
-	, m_lua_proto_index(lua::ref_none)
+	, m_lua_index(lua::ref::none)
+	, m_lua_proto_index(lua::ref::none)
 	, m_parent( nullptr )
 	, m_children()
Index: trunk/src/root.cc
===================================================================
--- trunk/src/root.cc	(revision 264)
+++ trunk/src/root.cc	(revision 265)
@@ -22,7 +22,7 @@
 	destroy_children( o );
 	o->detach();
-	if ( m_lua_state && o->m_lua_index != lua::ref_none )
+	if ( m_lua_state && o->m_lua_index != lua::ref::none )
 	{
-		m_lua_state->unregister_object( o->m_lua_index );
+		m_lua_state->unregister_object( lua::ref( o->m_lua_index ) );
 	}
 	if ( m_uid_store && o->m_uid != 0 )
@@ -47,9 +47,9 @@
 		if ( lua_name != nullptr )
 		{
-			o->m_lua_index       = m_lua_state->register_object( o, lua_name );
+			o->m_lua_index       = m_lua_state->register_object( o, lua_name ).get();
 		}
 		if ( storage != nullptr )
 		{
-			o->m_lua_proto_index = m_lua_state->register_proto( o->get_id().c_str(), storage );
+			o->m_lua_proto_index = m_lua_state->register_proto( o->get_id().c_str(), storage ).get();
 		}
 	}
