diff --git a/source/build/src/engine.cpp b/source/build/src/engine.cpp
index 56872b9cb..6399dcf3b 100644
--- a/source/build/src/engine.cpp
+++ b/source/build/src/engine.cpp
@@ -10920,7 +10920,7 @@ int32_t lastwall(int16_t point)
     if (point > 0 && wall[point-1].point2 == point)
         return point-1;
 
-    int i = point, cnt = MAXWALLS;
+    int i = point, cnt = numwalls;
     do
     {
         int const j = wall[i].point2;