diff --git a/game/engines/default/engine/Entity.lua b/game/engines/default/engine/Entity.lua
index a03cba7d1fda3aa583732cb83942df1f1c4472fb..584d80c9913890e532dc62049390a86f9d1ed169 100644
--- a/game/engines/default/engine/Entity.lua
+++ b/game/engines/default/engine/Entity.lua
@@ -29,23 +29,16 @@ module(..., package.seeall, class.make)
 local next_uid = 1
 local entities_load_functions = {}
 
-_M.__mo_repo = {}
 _M.__mo_final_repo = {}
 _M._no_save_fields = {}
 _M.__position_aware = false -- Subclasses can change it to know where they are on the map
 
 -- Setup the uids & MO repository as a weak value table, when the entities are no more used anywhere else they disappear from there too
 setmetatable(__uids, {__mode="v"})
-setmetatable(_M.__mo_repo, {__mode="k"})
 setmetatable(_M.__mo_final_repo, {__mode="k"})
 
 --- Invalidates the whole MO repository
 function _M:invalidateAllMO()
-	for mo, _ in pairs(_M.__mo_repo) do
-		mo:invalidate()
-	end
-	_M.__mo_repo = {}
-	setmetatable(_M.__mo_repo, {__mode="k"})
 	setmetatable(_M.__mo_final_repo, {__mode="k"})
 end
 
@@ -246,7 +239,6 @@ function _M:makeMapObject(tiles, idx)
 		self:check("display_h") or 1,
 		self:check("display_scale") or 1
 	)
-	_M.__mo_repo[self._mo] = true
 
 	self:defineDisplayCallback()
 
diff --git a/src/map.c b/src/map.c
index 554b9781081fde40659ec9f21eac380ed7b8b634..00e248c1ae805b4c5a5728ec722c893654f0332d 100644
--- a/src/map.c
+++ b/src/map.c
@@ -135,6 +135,7 @@ static int map_object_chain(lua_State *L)
 	map_object *obj2 = (map_object*)auxiliar_checkclass(L, "core{mapobj}", 2);
 	if (obj->next) return 0;
 	obj->next = obj2;
+	lua_pushvalue(L, 2);
 	obj->next_ref = luaL_ref(L, LUA_REGISTRYINDEX);
 	return 0;
 }
@@ -616,6 +617,7 @@ static int map_new(lua_State *L)
 	map->mwidth = mwidth;
 	map->mheight = mheight;
 	map->grids = calloc(w, sizeof(map_object***));
+	map->grids_ref = calloc(w, sizeof(int**));
 	map->grids_seens = calloc(w * h, sizeof(float));
 	map->grids_remembers = calloc(w, sizeof(bool*));
 	map->grids_lites = calloc(w, sizeof(bool*));
@@ -628,7 +630,12 @@ static int map_new(lua_State *L)
 	for (i = 0; i < w; i++)
 	{
 		map->grids[i] = calloc(h, sizeof(map_object**));
-		for (j = 0; j < h; j++) map->grids[i][j] = calloc(zdepth, sizeof(map_object*));
+		map->grids_ref[i] = calloc(h, sizeof(int*));
+		for (j = 0; j < h; j++)
+		{
+			map->grids[i][j] = calloc(zdepth, sizeof(map_object*));
+			map->grids_ref[i][j] = calloc(zdepth, sizeof(int));
+		}
 //		map->grids_seens[i] = calloc(h, sizeof(float));
 		map->grids_remembers[i] = calloc(h, sizeof(bool));
 		map->grids_lites[i] = calloc(h, sizeof(bool));
@@ -644,13 +651,19 @@ static int map_free(lua_State *L)
 
 	for (i = 0; i < map->w; i++)
 	{
-		for (j = 0; j < map->h; j++) free(map->grids[i][j]);
+		for (j = 0; j < map->h; j++)
+		{
+			free(map->grids[i][j]);
+			free(map->grids_ref[i][j]);
+		}
 		free(map->grids[i]);
+		free(map->grids_ref[i]);
 //		free(map->grids_seens[i]);
 		free(map->grids_remembers[i]);
 		free(map->grids_lites[i]);
 	}
 	free(map->grids);
+	free(map->grids_ref);
 	free(map->grids_seens);
 	free(map->grids_remembers);
 	free(map->grids_lites);
@@ -754,6 +767,8 @@ static int map_set_grid(lua_State *L)
 #endif
 			lua_pushnil(L);
 			lua_settable(L, 5); // Access the list of all mos for the map
+
+			luaL_unref(L, LUA_REGISTRYINDEX, map->grids_ref[x][y][i]);
 		}
 
 		lua_pushnumber(L, i + 1);
@@ -763,6 +778,8 @@ static int map_set_grid(lua_State *L)
 		{
 			map->grids[x][y][i]->cur_x = x;
 			map->grids[x][y][i]->cur_y = y;
+			lua_pushvalue(L, -1);
+			map->grids_ref[x][y][i] = luaL_ref(L, LUA_REGISTRYINDEX);
 		}
 
 		// Set the object in the mo list
diff --git a/src/map.h b/src/map.h
index 6b1c18ff153bcb9ac1bbdca232ac022b60639d66..d7312a2a687378d568dcffe4f8690d4dfc1843fa 100644
--- a/src/map.h
+++ b/src/map.h
@@ -59,6 +59,7 @@ typedef struct s_map_object map_object;
 
 typedef struct {
 	map_object* ***grids;
+	int ***grids_ref;
 	float *grids_seens;
 	bool **grids_remembers;
 	bool **grids_lites;