diff --git a/Rasterific.cabal b/Rasterific.cabal
--- a/Rasterific.cabal
+++ b/Rasterific.cabal
@@ -1,7 +1,7 @@
 -- Initial Rasterific.cabal generated by cabal init.  For further 
 -- documentation, see http://haskell.org/cabal/users-guide/
 name:                Rasterific
-version:             0.7.4.1
+version:             0.7.4.2
 synopsis:            A pure haskell drawing engine.
 -- A longer description of the package.
 description:
@@ -38,7 +38,7 @@
 Source-Repository this
     Type:      git
     Location:  git://github.com/Twinside/Rasterific.git
-    Tag:       v0.7.4.1
+    Tag:       v0.7.4.2
 
 flag embed_linear
   description: Embed a reduced version of Linear avoiding a (huge) dep
diff --git a/changelog b/changelog
--- a/changelog
+++ b/changelog
@@ -1,6 +1,14 @@
 Change log
 ==========
 
+v0.7.4.2 December 2018
+----------------------
+
+ * Refactoring: minor enhancement in the PathWalker
+   (newtype instead of single constructor data) (thanks to yairchu)
+ * Fix: PDF rendering didn't produce valid pdf in some cases,
+        the XRef table wasn't sorted by ID (thanks to robx)
+
 v0.7.4.1 October 2018
 ---------------------
 
diff --git a/src/Graphics/Rasterific/MicroPdf.hs b/src/Graphics/Rasterific/MicroPdf.hs
--- a/src/Graphics/Rasterific/MicroPdf.hs
+++ b/src/Graphics/Rasterific/MicroPdf.hs
@@ -17,6 +17,7 @@
 import Control.Monad.Reader( Reader, local, asks, runReader )
 
 import Numeric( showFFloat )
+import Data.List( sortOn )
 import Data.Monoid( (<>) )
 import qualified Data.Foldable as F
 import Data.Word( Word32 )
@@ -945,7 +946,7 @@
 buildXRefTable lst = tp "xref\n0 " <> intDec (glength lst) <> tp "\n"
                    <> foldMap build lst where
   build 0 = "0000000000 65535 f \n"
-  build ix = toPdf . B.pack $ printf "%010d 00000 n \n" ix
+  build off = toPdf . B.pack $ printf "%010d 00000 n \n" off
 
 buildTrailer :: Foldable f => f a -> PdfId -> Builder
 buildTrailer objs startId = tp "trailer\n" <> toPdf
@@ -1000,12 +1001,13 @@
     <> reverse (_generatedPdfObjects endContext)
 
   (indexes, objs) = unzip $ prepareObjects objects
-  lastIndex = last indexes
-  xrefIndex = lastIndex + B.length (last objs)
+  (_, lastOffset) = last indexes
+  xrefOffset = lastOffset + B.length (last objs)
 
-  xrefPosition = "startxref\n" <> intDec xrefIndex <> tp "\n"
+  xrefPosition = "startxref\n" <> intDec xrefOffset <> tp "\n"
 
-  xref = buildXRefTable indexes
+  offsets = map snd $ sortOn fst indexes
+  xref = buildXRefTable offsets
 
 renderDrawingToPdf :: (forall px . PdfColorable px => Drawing px () -> [DrawOrder px])
                    -> Int -> Int -> Dpi -> Drawing PixelRGBA8 ()
@@ -1165,7 +1167,6 @@
       , _pdfConfToOrder = toOrders
       }
 
-prepareObjects :: [PdfObject] -> [(Int, B.ByteString)]
-prepareObjects = scanl go (0, pdfSignature) where
-  go (ix, prev) obj = (ix + B.length prev, buildToStrict $ toPdf obj)
-
+prepareObjects :: [PdfObject] -> [((PdfId, Int), B.ByteString)]
+prepareObjects = scanl go ((0, 0), pdfSignature) where
+  go ((_, off), prev) obj = ((_pdfId obj, off + B.length prev), buildToStrict $ toPdf obj)
diff --git a/src/Graphics/Rasterific/PathWalker.hs b/src/Graphics/Rasterific/PathWalker.hs
--- a/src/Graphics/Rasterific/PathWalker.hs
+++ b/src/Graphics/Rasterific/PathWalker.hs
@@ -41,8 +41,8 @@
 -- | State of the path walker, just a bunch of primitives
 -- with continuity guarantee. The continuity is guaranteed
 -- by the Path used to derive this primitives.
-data WalkerState = WalkerState
-    { _walkerPrims :: ![Primitive]
+newtype WalkerState = WalkerState
+    { _walkerPrims :: [Primitive]
     }
 
 -- | Create a path walker from a given path
