diff --git a/src/taglist.c b/src/taglist.c
index 087f069c449f1904eab7a001c363814cdc18b15e..2e91cd7709049177dbe593d85daff4061e26ea73 100644
--- a/src/taglist.c
+++ b/src/taglist.c
@@ -1,5 +1,6 @@
 #include "taglist.h"
 #include "z_zone.h"
+#include "r_data.h"
 
 void Tag_Add (taglist_t* list, const UINT16 tag)
 {
@@ -56,3 +57,57 @@ void Taglist_AddToMapthings (const size_t tag, const size_t itemid)
 	tagelems->elements = Z_Realloc(tagelems->elements, tagelems->count * sizeof(size_t), PU_LEVEL, NULL);
 	tagelems->elements[tagelems->count - 1] = itemid;
 }
+
+INT32 Tag_Iterate_Sectors (const INT16 tag, const size_t p)
+{
+	if (tag == -1)
+	{
+		if (p < numsectors)
+			return p;
+		return -1;
+	}
+
+	if (tags_sectors[tag])
+	{
+		if (p < tags_sectors[tag]->count)
+			return tags_sectors[tag]->elements[p];
+		return -1;
+	}
+	return -1;
+}
+
+INT32 Tag_Iterate_Lines (const INT16 tag, const size_t p)
+{
+	if (tag == -1)
+	{
+		if (p < numlines)
+			return p;
+		return -1;
+	}
+
+	if (tags_lines[tag])
+	{
+		if (p < tags_lines[tag]->count)
+			return tags_lines[tag]->elements[p];
+		return -1;
+	}
+	return -1;
+}
+
+INT32 Tag_Iterate_Things (const INT16 tag, const size_t p)
+{
+	if (tag == -1)
+	{
+		if (p < nummapthings)
+			return p;
+		return -1;
+	}
+
+	if (tags_mapthings[tag])
+	{
+		if (p < tags_mapthings[tag]->count)
+			return tags_mapthings[tag]->elements[p];
+		return -1;
+	}
+	return -1;
+}
diff --git a/src/taglist.h b/src/taglist.h
index 5dab7315fe82e75baf21e3c25e2dd2f228b7b8e0..ca3a7d834ca8325a0da7f2716f22e50b49de5fc1 100644
--- a/src/taglist.h
+++ b/src/taglist.h
@@ -1,8 +1,8 @@
-#include "doomtype.h"
-
 #ifndef __R_TAGLIST__
 #define __R_TAGLIST__
 
+#include "doomtype.h"
+
 /// Multitag list.
 typedef struct
 {
@@ -27,16 +27,16 @@ taggroup_t* tags_mapthings[MAXTAGS];
 void Taglist_AddToSectors (const size_t tag, const size_t itemid);
 void Taglist_AddToLines (const size_t tag, const size_t itemid);
 void Taglist_AddToMapthings (const size_t tag, const size_t itemid);
-#endif //__R_TAGLIST__
+
+INT32 Tag_Iterate_Sectors (const INT16 tag, const size_t p);
+INT32 Tag_Iterate_Lines (const INT16 tag, const size_t p);
+INT32 Tag_Iterate_Things (const INT16 tag, const size_t p);
 
 #define TAG_ITER_C size_t kkkk;
+#define TAG_ITER(fn, tag, id) for(kkkk = 0; (id = fn(tag, kkkk)) >= 0; kkkk++)
 
-#define TAG_ITER(group, grouptotal, tag, id)\
-if (group[tag] || tag == -1) for(\
-	tag != -1 ? (id = group[tag]->elements[kkkk = 0]) : (id = 0);\
-	tag != -1 ? (kkkk < group[tag]->count)            : (id < grouptotal);\
-	tag != -1 ? (id = group[tag]->elements[++kkkk])   : (id++))
+#define TAG_ITER_SECTORS(tag, id) TAG_ITER(Tag_Iterate_Sectors, tag, id)
+#define TAG_ITER_LINES(tag, id)   TAG_ITER(Tag_Iterate_Lines, tag, id)
+#define TAG_ITER_THINGS(tag, id)  TAG_ITER(Tag_Iterate_Things, tag, id)
 
-#define TAG_ITER_SECTORS(tag, id) TAG_ITER(tags_sectors, numsectors, tag, id)
-#define TAG_ITER_LINES(tag, id)   TAG_ITER(tags_lines, numlines, tag, id)
-#define TAG_ITER_THINGS(tag, id)  TAG_ITER(tags_mapthings, nummapthings, tag, id)
+#endif //__R_TAGLIST__