diff --git a/src/deh_lua.c b/src/deh_lua.c
index 0b789547b266aeb238701ca6b34c4f8702c334fb..7188d25e369b5c4a42c1f6d06568c133d794fc2c 100644
--- a/src/deh_lua.c
+++ b/src/deh_lua.c
@@ -771,8 +771,7 @@ int LUA_SOCLib(lua_State *L)
 	lua_register(L,"getActionName",lib_getActionName);
 
 	luaL_newmetatable(L, META_ACTION);
-		lua_pushcfunction(L, action_call);
-		lua_setfield(L, -2, "__call");
+		LUA_SetCFunctionField(L, "__call", action_call);
 	lua_pop(L, 1);
 
 	return 0;
diff --git a/src/lua_baselib.c b/src/lua_baselib.c
index 1821550b86ff194995346ea7a4fbb4479444e2bb..f7ed763a9cebaf66897cca7f60bdd93fe7ded1e4 100644
--- a/src/lua_baselib.c
+++ b/src/lua_baselib.c
@@ -4300,8 +4300,7 @@ int LUA_BaseLib(lua_State *L)
 	// Set metatable for string
 	lua_pushliteral(L, "");  // dummy string
 	lua_getmetatable(L, -1);  // get string metatable
-	lua_pushcfunction(L,lib_concat); // push concatination function
-	lua_setfield(L,-2,"__add"); // ... store it as mathematical addition
+	LUA_SetCFunctionField(L, "__add", lib_concat);
 	lua_pop(L, 2); // pop metatable and dummy string
 
 	lua_newtable(L);
diff --git a/src/lua_maplib.c b/src/lua_maplib.c
index 2e1aab3eb4d9f19df84cde3ae5cbbdb384cd07bf..efa2edf20befac5ee27061ebbbb61922ebc24c91 100644
--- a/src/lua_maplib.c
+++ b/src/lua_maplib.c
@@ -2898,10 +2898,8 @@ int LUA_MapLib(lua_State *L)
 	node_fields_ref = Lua_CreateFieldTable(L, node_opt);
 
 	luaL_newmetatable(L, META_NODEBBOX);
-		//lua_pushcfunction(L, nodebbox_get);
-		//lua_setfield(L, -2, "__index");
-		lua_pushcfunction(L, nodebbox_call);
-		lua_setfield(L, -2, "__call");
+		//LUA_SetCFunctionField(L, "__index", nodebbox_get);
+		LUA_SetCFunctionField(L, "__call", nodebbox_call);
 	lua_pop(L, 1);
 
 	LUA_RegisterGlobalUserdata(L, "segs", lib_getSeg, NULL, lib_numsegs);
diff --git a/src/lua_script.c b/src/lua_script.c
index 9aea96ac73b59928367f63efc7b649fb030e3b06..5cacc3f01bcad77d41c33ac101a7b48795695fde 100644
--- a/src/lua_script.c
+++ b/src/lua_script.c
@@ -578,8 +578,7 @@ static void LUA_ClearState(void)
 
 	// lock the global namespace
 	lua_getmetatable(L, LUA_GLOBALSINDEX);
-		lua_pushcfunction(L, setglobals);
-		lua_setfield(L, -2, "__newindex");
+		LUA_SetCFunctionField(L, "__newindex", setglobals);
 		lua_newtable(L);
 		lua_setfield(L, -2, "__metatable");
 	lua_pop(L, 1);
@@ -1815,20 +1814,17 @@ void LUA_PushTaggableObjectArray
 	lua_newuserdata(L, 0);
 		lua_createtable(L, 0, 2);
 			lua_createtable(L, 0, 2);
-				lua_pushcfunction(L, iterator);
-				lua_setfield(L, -2, "iterate");
+				LUA_SetCFunctionField(L, "iterate", iterator);
 
 				LUA_InsertTaggroupIterator(L, garray,
 						max_elements, element_array, sizeof_element, meta);
 
 				lua_createtable(L, 0, 1);
-					lua_pushcfunction(L, indexer);
-					lua_setfield(L, -2, "__index");
+					LUA_SetCFunctionField(L, "__index", indexer);
 				lua_setmetatable(L, -2);
 			lua_setfield(L, -2, "__index");
 
-			lua_pushcfunction(L, counter);
-			lua_setfield(L, -2, "__len");
+			LUA_SetCFunctionField(L, "__len", counter);
 		lua_setmetatable(L, -2);
 	lua_setglobal(L, field);
 }
@@ -1841,20 +1837,17 @@ static void SetBasicMetamethods(
 )
 {
 	if (get)
-	{
-		lua_pushcfunction(L, get);
-		lua_setfield(L, -2, "__index");
-	}
+		LUA_SetCFunctionField(L, "__index", get);
 	if (set)
-	{
-		lua_pushcfunction(L, set);
-		lua_setfield(L, -2, "__newindex");
-	}
+		LUA_SetCFunctionField(L, "__newindex", set);
 	if (len)
-	{
-		lua_pushcfunction(L, len);
-		lua_setfield(L, -2, "__len");
-	}
+		LUA_SetCFunctionField(L, "__len", len);
+}
+
+void LUA_SetCFunctionField(lua_State *L, const char *name, lua_CFunction value)
+{
+	lua_pushcfunction(L, value);
+	lua_setfield(L, -2, name);
 }
 
 void LUA_RegisterUserdataMetatable(
diff --git a/src/lua_script.h b/src/lua_script.h
index dd9a2568e0d197a9c53841cd1c7b165160cf4827..53d848f8e878a4234b49fd8e38bbc94325488f61 100644
--- a/src/lua_script.h
+++ b/src/lua_script.h
@@ -73,6 +73,8 @@ void LUA_PushTaggableObjectArray
 		size_t sizeof_element,
 		const char *meta);
 
+void LUA_SetCFunctionField(lua_State *L, const char *name, lua_CFunction value);
+
 void LUA_RegisterUserdataMetatable(
 	lua_State *L,
 	const char *name,
diff --git a/src/lua_taglib.c b/src/lua_taglib.c
index 5ed457534ca24c25f55e6a54f0702eb859f7cdf6..9e73a050c9011f796be0a2ae41399d79f2a7f836 100644
--- a/src/lua_taglib.c
+++ b/src/lua_taglib.c
@@ -372,8 +372,7 @@ void LUA_InsertTaggroupIterator
 		lua_pushcclosure(L, lib_numTaggroupElements, 2);
 		lua_setfield(L, -2, "__len");
 
-		lua_pushcfunction(L, element_iterator);
-		lua_setfield(L, -2, "__call");
+		LUA_SetCFunctionField(L, "__call", element_iterator);
 	lua_pushcclosure(L, lib_getTaggroup, 1);
 	lua_setfield(L, -2, "tagged");
 }
@@ -414,11 +413,9 @@ set_taglist_metatable(lua_State *L, const char *meta)
 		lua_setfenv(L, -2);
 		lua_setfield(L, -2, "__index");
 
-		lua_pushcfunction(L, taglist_len);
-		lua_setfield(L, -2, "__len");
+		LUA_SetCFunctionField(L, "__len", taglist_len);
 
-		lua_pushcfunction(L, taglist_equal);
-		lua_setfield(L, -2, "__eq");
+		LUA_SetCFunctionField(L, "__eq", taglist_equal);
 #ifdef MUTABLE_TAGS
 	return luaL_ref(L, LUA_REGISTRYINDEX);
 #endif
@@ -428,8 +425,7 @@ int LUA_TagLib(lua_State *L)
 {
 	LUA_CreateAndSetUserdataField(L, LUA_GLOBALSINDEX, "tags", NULL, NULL, lib_numTags, true);
 		lua_createtable(L, 0, 1);
-			lua_pushcfunction(L, lib_iterateTags);
-			lua_setfield(L, -2, "iterate");
+			LUA_SetCFunctionField(L, "iterate", lib_iterateTags);
 		lua_setfield(L, -2, "__index");
 	lua_pop(L, 2);
 
diff --git a/src/lua_thinkerlib.c b/src/lua_thinkerlib.c
index cff92f34d6e0a98dff5b1c76cc97ff358c11ff7d..f1be8c78933bab543d00388ae76b40512742d9aa 100644
--- a/src/lua_thinkerlib.c
+++ b/src/lua_thinkerlib.c
@@ -127,8 +127,7 @@ static int lib_startIterate(lua_State *L)
 int LUA_ThinkerLib(lua_State *L)
 {
 	luaL_newmetatable(L, META_ITERATIONSTATE);
-	lua_pushcfunction(L, iterationState_gc);
-	lua_setfield(L, -2, "__gc");
+	LUA_SetCFunctionField(L, "__gc", iterationState_gc);
 	lua_pop(L, 1);
 
 	lua_createtable(L, 0, 1);