diff --git a/game.cpp b/game.cpp
index 17e51ba..2b35924 100644
--- a/game.cpp
+++ b/game.cpp
@@ -379,13 +379,15 @@ void drawSprite(const unsigned char *sprite, int16_t x, int16_t y, Unit depth, i
   {
     for (Unit i = max(-1 * x,0); i < iTo; ++i)
     {
-      if (!mask[i])
+      int16_t xPos = x + i;
+
+      if (!mask[xPos / SUBSAMPLE])
         continue;
 
       c = sampleImage(sprite,(i * UNITS_PER_SQUARE) / size,(j * UNITS_PER_SQUARE) / size); 
      
       if (c != 0xff)
-        p.display.drawPixel(x + i,y + j,c);
+        p.display.drawPixel(xPos,y + j,c);
     }
   }
 }
@@ -528,6 +530,8 @@ void draw()
 
   render(player.mCamera,heightFunc,pixelFunc,c);
 
+  Unit previousDepth;
+
   for (uint8_t i = 0; i < SPRITES; ++i)
   {
     PixelInfo pos = mapToScreen(sprites[i].mPosition,sprites[i].mHeight,player.mCamera);
@@ -535,6 +539,15 @@ void draw()
     if (pos.depth > 0)
       drawSprite(sprite,pos.position.x * SUBSAMPLE,pos.position.y,
                  pos.depth,perspectiveScale(32,pos.depth));
+
+    if (i != 0 && pos.depth > previousDepth)
+    {
+      Sprite tmp = sprites[i];
+      sprites[i] = sprites[i - 1];
+      sprites[i - 1] = tmp;
+    }
+
+    previousDepth = pos.depth;
   }
 }