diff --git a/Runtime/VectorTile.cs b/Runtime/VectorTile.cs
index 185b3fee2e511a302f27db7c4fa7e0646e28a0a1..6c2b997415fe02bbdef5a750794ba151b73e8238 100644
--- a/Runtime/VectorTile.cs
+++ b/Runtime/VectorTile.cs
@@ -408,6 +408,8 @@ namespace Wander
             UnityEngine.Debug.Assert( resolution > 1, "Must be at least 2." );
 
             byte [] texture = new byte[resolution*resolution/2];
+            bool [] written = new bool[resolution*resolution];
+
             int d = 4096 / resolution;
             int shift = -1;
             while (d!=0)
@@ -430,6 +432,7 @@ namespace Wander
                         {
                             var min = poly.mins[t];
                             var max = poly.maxs[t];
+
                             int x1 = Mathf.FloorToInt( min.x );
                             int y1 = Mathf.FloorToInt( min.y );
                             int x2 = Mathf.FloorToInt( max.x );
@@ -462,11 +465,14 @@ namespace Wander
                                     if (PointInTriangle( new Vector2( xk2, yk2 ), poly.vertices[i], poly.vertices[i+1], poly.vertices[i+2] ))
                                     {
                                         var addr2 = (resolution - y -1)*resolution+x;
-                                        //var addr2 = (resolution* y)+x;
-                                        if (layerIdx > 15) 
-                                            layerIdx = 15; // max layers 15 (15 is reserved, nothing was hit). 
-                                        var shift2 = (addr2&1) << 2;
-                                        texture[addr2>>1] |= (byte)(layerIdx << shift2);
+                                        if (!written[addr2])
+                                        {
+                                            written[addr2]=true;
+                                            if (layerIdx > 15)
+                                                layerIdx = 15; // max layers 15 (15 is reserved, nothing was hit).
+                                            var shift2 = (addr2&1) << 2;
+                                            texture[addr2>>1] |= (byte)(layerIdx << shift2);
+                                        }
                                     }
                                 }
                             }
@@ -600,8 +606,9 @@ namespace Wander
                 {
                     if ((x&1) == 0)
                         pixel = texture[addr>>1];
+                    UnityEngine.Debug.Assert( (pixel&0xF) < 10 && ((pixel>>4)&0xF) < 10 );
                     byte shift  = (byte)((addr&1));
-                    byte pixel2 = (byte)((pixel >> (shift<<2)) & 15);
+                    byte pixel2 = (byte)((pixel >> (shift<<2)) & 0xF);
                     if (pixel2 != 15)
                     {
                         if (pixel2 != cachedPixel)