diff --git a/src/hardware/hw_defs.h b/src/hardware/hw_defs.h
index 70d776d9e962c219d3449bdd3db262e6017962db..9011e8e64baf9020e158514970c594935fba49a3 100644
--- a/src/hardware/hw_defs.h
+++ b/src/hardware/hw_defs.h
@@ -22,6 +22,7 @@
 
 #define ZCLIP_PLANE 4.0f
 #define NZCLIP_PLANE 0.9f
+#define ALAM_LIGHTING
 
 // ==========================================================================
 //                                                               SIMPLE TYPES
diff --git a/src/hardware/hw_light.c b/src/hardware/hw_light.c
index ee68edd41be09cc1922b9faec951c4087d1b5120..ffabcc020532d1b34ad14784dc4798ddb750c1b7 100644
--- a/src/hardware/hw_light.c
+++ b/src/hardware/hw_light.c
@@ -761,8 +761,10 @@ void HWR_WallLighting(FOutVector *wlVerts)
 #ifdef DL_HIGH_QUALITY
 		Surf.FlatColor.s.alpha = (UINT8)((1-dist_p2d/DL_SQRRADIUS(j))*Surf.FlatColor.s.alpha);
 #endif
+		if ((!dynlights->mo[j]) || (dynlights->mo[j]->thinker.function.acp1 != (actionf_p1)P_MobjThinker))
+			continue;
 		if (!dynlights->mo[j]->state)
-			return;
+			continue;
 		// next state is null so fade out with alpha
 		if (dynlights->mo[j]->state->nextstate == S_NULL)
 			Surf.FlatColor.s.alpha = (UINT8)(((float)dynlights->mo[j]->tics/(float)dynlights->mo[j]->state->tics)*Surf.FlatColor.s.alpha);
@@ -824,8 +826,10 @@ void HWR_PlaneLighting(FOutVector *clVerts, int nrClipVerts)
 #ifdef DL_HIGH_QUALITY
 		Surf.FlatColor.s.alpha = (unsigned char)((1 - dist_p2d/DL_SQRRADIUS(j))*Surf.FlatColor.s.alpha);
 #endif
+		if ((!dynlights->mo[j]) || (dynlights->mo[j]->thinker.function.acp1 != (actionf_p1)P_MobjThinker))
+			continue;
 		if (!dynlights->mo[j]->state)
-			return;
+			continue;
 		// next state is null so fade out with alpha
 		if ((dynlights->mo[j]->state->nextstate == S_NULL))
 			Surf.FlatColor.s.alpha = (unsigned char)(((float)dynlights->mo[j]->tics/(float)dynlights->mo[j]->state->tics)*Surf.FlatColor.s.alpha);
@@ -1231,12 +1235,37 @@ static void HWR_CheckSubsector(size_t num, fixed_t *bbox)
 }
 
 
+//Hurdler: The goal of this function is to walk through all the bsp starting
+//         on the top.
+//         We need to do that to know all the lights in the map and all the walls
+static void HWR_ComputeLightMapsInBSPNode(int bspnum, fixed_t *bbox)
+{
+	if (bspnum & NF_SUBSECTOR) // Found a subsector?
+	{
+		if (bspnum == -1)
+			HWR_CheckSubsector(0, bbox);  // probably unecessary: see boris' comment in hw_bsp
+		else
+			HWR_CheckSubsector(bspnum&(~NF_SUBSECTOR), bbox);
+		return;
+	}
+	HWR_ComputeLightMapsInBSPNode(nodes[bspnum].children[0], nodes[bspnum].bbox[0]);
+	HWR_ComputeLightMapsInBSPNode(nodes[bspnum].children[1], nodes[bspnum].bbox[1]);
+}
+#endif
+
 // --------------------------------------------------------------------------
 // Hurdler: this adds lights by mobj.
 // --------------------------------------------------------------------------
 static void HWR_AddMobjLights(mobj_t *thing)
 {
-	if (t_lspr[thing->sprite]->type & CORONA_SPR)
+    if (!cv_drawdist.value || P_AproxDistance(thing->x-viewx, thing->y-viewy) < cv_drawdist.value)
+	if (!(thing->flags2 & MF2_DEBRIS) && (thing->sprite != SPR_PLAY ||
+	 (thing->player && thing->player->powers[pw_super])))
+	if ((t_lspr[thing->sprite]->type&DYNLIGHT_SPR)
+	  && ((t_lspr[thing->sprite]->type != LIGHT_SPR) || cv_grstaticlighting.value)
+	  && (dynlights->nb < DL_MAX_LIGHT)
+
+	  && thing->state)
 	{
 		LIGHT_POS(dynlights->nb).x = FIXED_TO_FLOAT(thing->x);
 		LIGHT_POS(dynlights->nb).y = FIXED_TO_FLOAT(thing->z) + t_lspr[thing->sprite]->light_yoffset;
@@ -1244,30 +1273,15 @@ static void HWR_AddMobjLights(mobj_t *thing)
 
 		dynlights->p_lspr[dynlights->nb] = t_lspr[thing->sprite];
 
+		P_SetTarget(&dynlights->mo[dynlights->nb], thing);
+
 		dynlights->nb++;
 		if (dynlights->nb > DL_MAX_LIGHT)
 			dynlights->nb = DL_MAX_LIGHT;
 	}
 }
 
-//Hurdler: The goal of this function is to walk through all the bsp starting
-//         on the top.
-//         We need to do that to know all the lights in the map and all the walls
-static void HWR_ComputeLightMapsInBSPNode(int bspnum, fixed_t *bbox)
-{
-	if (bspnum & NF_SUBSECTOR) // Found a subsector?
-	{
-		if (bspnum == -1)
-			HWR_CheckSubsector(0, bbox);  // probably unecessary: see boris' comment in hw_bsp
-		else
-			HWR_CheckSubsector(bspnum&(~NF_SUBSECTOR), bbox);
-		return;
-	}
-	HWR_ComputeLightMapsInBSPNode(nodes[bspnum].children[0], nodes[bspnum].bbox[0]);
-	HWR_ComputeLightMapsInBSPNode(nodes[bspnum].children[1], nodes[bspnum].bbox[1]);
-}
-
-static void HWR_SearchLightsInMobjs(void)
+void HWR_SearchLightsInMobjs(void)
 {
 	thinker_t *         th;
 	//mobj_t *            mobj;
@@ -1278,9 +1292,15 @@ static void HWR_SearchLightsInMobjs(void)
 		// a mobj ?
 		if (th->function.acp1 == (actionf_p1)P_MobjThinker)
 			HWR_AddMobjLights((mobj_t *)th);
+
+
+		if (dynlights->nb == DL_MAX_LIGHT)
+        {
+            CONS_Printf("light limit exceeded\n");
+            return;
+        }
 	}
 }
-#endif
 
 //
 // HWR_CreateStaticLightmaps()
diff --git a/src/hardware/hw_light.h b/src/hardware/hw_light.h
index 2733cc698e9f4cf9d8f5e5a715dbcad9ab8a8875..f3085e7adc63225c133728e369cc93ca019e8018 100644
--- a/src/hardware/hw_light.h
+++ b/src/hardware/hw_light.h
@@ -36,6 +36,8 @@ void HWR_WallLighting(FOutVector *wlVerts);
 void HWR_ResetLights(void);
 void HWR_SetLights(int viewnumber);
 
+void HWR_SearchLightsInMobjs(void);
+
 #ifdef NEWCORONAS
 void HWR_DrawCoronas(void);
 #else
diff --git a/src/hardware/hw_main.c b/src/hardware/hw_main.c
index 04175d50496827f3f0940ecefe9e3fd1d8625842..04f3b820ee0e4026f5f24764ad6d633795e7bece 100644
--- a/src/hardware/hw_main.c
+++ b/src/hardware/hw_main.c
@@ -3560,7 +3560,7 @@ static void HWR_DrawSprite(gr_vissprite_t *spr)
 
 	gpatch = W_CachePatchNum(spr->patchlumpnum, PU_CACHE);
 
-#ifdef ALAM_LIGHTING
+#if 0 //#ifdef ALAM_LIGHTING
 	if (!(spr->mobj->flags2 & MF2_DEBRIS) && (spr->mobj->sprite != SPR_PLAY ||
 	 (spr->mobj->player && spr->mobj->player->powers[pw_super])))
 		HWR_DL_AddLight(spr, gpatch);
@@ -5073,6 +5073,12 @@ if (0)
 
 	validcount++;
 
+#ifdef ALAM_LIGHTING
+	//14/11/99: Hurdler: moved here because it doesn't work with
+	// subsector, see other comments;
+	HWR_ResetLights();
+    HWR_SearchLightsInMobjs();
+#endif
 	HWR_RenderBSPNode((INT32)numnodes-1);
 
 	// Make a viewangle int so we can render things based on mouselook
@@ -5108,7 +5114,7 @@ if (0)
 #ifdef ALAM_LIGHTING
 	//14/11/99: Hurdler: moved here because it doesn't work with
 	// subsector, see other comments;
-	HWR_ResetLights();
+	//HWR_ResetLights();
 #endif
 
 	// Draw MD2 and sprites
@@ -5303,6 +5309,13 @@ if (0)
 
 	validcount++;
 
+
+#ifdef ALAM_LIGHTING
+	//14/11/99: Hurdler: moved here because it doesn't work with
+	// subsector, see other comments;
+	HWR_ResetLights();
+    HWR_SearchLightsInMobjs();
+#endif
 	HWR_RenderBSPNode((INT32)numnodes-1);
 
 	// Make a viewangle int so we can render things based on mouselook
@@ -5335,12 +5348,6 @@ if (0)
 	// Check for new console commands.
 	NetUpdate();
 
-#ifdef ALAM_LIGHTING
-	//14/11/99: Hurdler: moved here because it doesn't work with
-	// subsector, see other comments;
-	HWR_ResetLights();
-#endif
-
 	// Draw MD2 and sprites
 #ifdef SORTING
 	HWR_SortVisSprites();