Index: trunk/nv/lua/lua_state.hh
===================================================================
--- trunk/nv/lua/lua_state.hh	(revision 73)
+++ trunk/nv/lua/lua_state.hh	(revision 74)
@@ -30,6 +30,7 @@
 		public:
 			stack_guard( state* L );
+			int get_level() const { return m_level; }
 			~stack_guard();
-		public:
+		private:
 			state* L;
 			int m_level;
@@ -60,5 +61,5 @@
 			friend class table_guard;
 		public:
-			state( bool is_main = false );
+			state( bool load_libs = false );
 			state( lua_State* state );
 			int load_string( const std::string& code, const std::string& name );
@@ -72,5 +73,4 @@
 			lua_State* get_raw();
 			~state();
-			static state* get();
 		private:
 			int do_current( const std::string& name );
Index: trunk/src/lua/lua_glm.cc
===================================================================
--- trunk/src/lua/lua_glm.cc	(revision 73)
+++ trunk/src/lua/lua_glm.cc	(revision 74)
@@ -335,4 +335,5 @@
 	nlua_swizzel_lookup['v'] = 0;
 	nlua_swizzel_lookup['3'] = 3;
+	int stack = lua_gettop( L );
 	luaL_requiref(L, "ivec2", luaopen_vec<glm::ivec2>, 1);
 	luaL_requiref(L, "ivec3", luaopen_vec<glm::ivec3>, 1);
@@ -341,4 +342,5 @@
 	luaL_requiref(L, "vec3", luaopen_vec<glm::vec3>, 1);
 	luaL_requiref(L, "vec4", luaopen_vec<glm::vec4>, 1);
-}
-
+	lua_settop( L, stack );
+}
+
Index: trunk/src/lua/lua_state.cc
===================================================================
--- trunk/src/lua/lua_state.cc	(revision 73)
+++ trunk/src/lua/lua_state.cc	(revision 74)
@@ -24,6 +24,7 @@
 }
 
-lua::state::state( bool is_main /*= false*/ )
-{
+lua::state::state( bool load_libs /*= false*/ )
+{
+	load_lua_library();
 	m_owner = true;
 	L = luaL_newstate( );
@@ -33,15 +34,22 @@
 	lua_call(L, 1, 0);
 
-	if (is_main)
-	{
-		NV_ASSERT( this == get(), "lua_state : another main state created!" );
+	if ( load_libs )
+	{
 		stack_guard guard( this );
-		luaopen_base( L );
-		luaopen_string( L );
-		luaopen_table( L );
-		luaopen_math( L );
-	}
-
-	NV_LOG( nv::LOG_TRACE, is_main ? "Main Lua state created" : "Secondary Lua state created");
+		static const luaL_Reg lualibs[] =
+		{
+			{ "string", luaopen_string },
+			{ "table",  luaopen_table },
+			{ "math",   luaopen_math },
+			{ NULL, NULL}
+		};
+		const luaL_Reg *lib = lualibs;
+		for(; lib->func != NULL; lib++)
+		{
+			lib->func( L );
+		}
+	}
+
+	NV_LOG( nv::LOG_TRACE, "Lua state created" );
 }
 
@@ -121,10 +129,4 @@
 }
 
-lua::state* lua::state::get()
-{
-	static lua::state main_state(true);
-	return &main_state;
-}
-
 bool lua::state::push( const std::string& path, bool global )
 {
@@ -275,5 +277,5 @@
 	for ( int i = 0; i < top; ++i )
 	{
-		NV_LOG( LOG_DEBUG, "#" << i+1 << " - " << lua_typename(L, i+1) << " = " << nlua_typecontent(L, i+1) );
+		NV_LOG( LOG_DEBUG, "#" << i+1 << " - " << lua_typename(L, lua_type(L, i+1) ) << " = " << nlua_typecontent(L, i+1) );
 	}
 }
Index: trunk/tests/lualib_test/lualib_test.cc
===================================================================
--- trunk/tests/lualib_test/lualib_test.cc	(revision 73)
+++ trunk/tests/lualib_test/lualib_test.cc	(revision 74)
@@ -1,3 +1,4 @@
 #include <nv/lib/lua.hh>
+#include <nv/lua/lua_state.hh>
 #include <nv/lua/lua_raw.hh>
 #include <nv/lua/lua_glm.hh>
@@ -31,78 +32,57 @@
 	db.create_type<test_struct>().fields( fields );
 
-
 	nv::logger log(nv::LOG_TRACE);
 	log.add_sink( new nv::log_file_sink("log.txt"), nv::LOG_TRACE );
 	log.add_sink( new nv::log_console_sink(), nv::LOG_TRACE );
-	nv::load_lua_library();
 	
 	NV_LOG( nv::LOG_NOTICE, "Logging started" );
 
 	// create new Lua state
-	lua_State *lua_state;
-	lua_state = luaL_newstate();
+	{
+		nv::lua::state state( true );
+		state.log_stack();
+		nlua_register_glm( state.get_raw() );
+		// run the Lua script
+		state.do_file( "init.lua" );
 
-	// load Lua libraries
-	static const luaL_Reg lualibs[] =
-	{
-		{ "base", luaopen_base },
-		{ NULL, NULL}
-	};
+		for (;;)
+		{
+			nv::lua::stack_guard guard( &state );
+			int stack = guard.get_level();
+			std::string input;
+			std::cout << "LUA (" << stack << ") > ";
+			std::getline( std::cin, input );
+			if (input == "quit")
+			{
+				break;
+			}
 
-	nlua_register_glm( lua_state );
+			if (input.find("=", 0) == std::string::npos && 
+				input.find("if", 0) == std::string::npos && 
+				input.find("return", 0) == std::string::npos)
+			{
+				input = "return " + input;
+			}
 
-	const luaL_Reg *lib = lualibs;
-	for(; lib->func != NULL; lib++)
-	{
-		lib->func(lua_state);
-		lua_settop(lua_state, 0);
-	}
+			std::cout << "> " << input << std::endl;
 
-	// run the Lua script
-	luaL_dofile(lua_state, "init.lua");
+			int code = luaL_loadstring( state.get_raw(), input.c_str() );
+			if (code == 0) code = lua_pcall( state.get_raw(), 0, LUA_MULTRET, 0);
+			if (code != 0)
+			{
+				std::string error = lua_tostring( state.get_raw(), -1 );
+				std::cout << "ERROR : " << error << std::endl;
+				continue;
+			}
 
-	for (;;)
-	{
-		std::string input;
-		int stack = lua_gettop( lua_state );
-		std::cout << "LUA (" << stack << ") > ";
-		std::getline( std::cin, input );
-		if (input == "quit")
-		{
-			break;
-		}
-
-		if (input.find("=", 0) == std::string::npos && 
-			input.find("if", 0) == std::string::npos && 
-			input.find("return", 0) == std::string::npos)
-		{
-			input = "return " + input;
-		}
-
-		std::cout << "> " << input << std::endl;
-
-		int code = luaL_loadstring( lua_state, input.c_str() );
-		if (code == 0) code = lua_pcall( lua_state, 0, LUA_MULTRET, 0);
-		if (code != 0)
-		{
-			std::string error = lua_tostring( lua_state, -1 );
-			std::cout << "ERROR : " << error << std::endl;
-			lua_settop( lua_state, stack );
-			continue;
-		}
-
-		if (lua_gettop( lua_state ) > stack)
-		{
-			for ( int i = stack+1; i <= lua_gettop( lua_state ); ++i )
+			if (lua_gettop( state.get_raw() ) > stack)
 			{
-				std::cout << nlua_typecontent( lua_state, i ) << std::endl;
+				for ( int i = stack+1; i <= lua_gettop( state.get_raw() ); ++i )
+				{
+					std::cout << nlua_typecontent( state.get_raw(), i ) << std::endl;
+				}
 			}
 		}
-		lua_settop( lua_state, stack );
 	}
-
-
-	// close the Lua state
-	lua_close(lua_state);
 	NV_LOG( nv::LOG_NOTICE, "Logging stopped" );
 
