diff --git a/Data/Object/Yaml.hs b/Data/Object/Yaml.hs
--- a/Data/Object/Yaml.hs
+++ b/Data/Object/Yaml.hs
@@ -89,8 +89,8 @@
 mergeAssocLists [] b = b
 mergeAssocLists a ((bk, bv):bs) =
     case lookup bk a of
-      Nothing -> mergeAssocLists ((bk, bv) : a) bs
-      Just _  -> mergeAssocLists a bs
+      Nothing -> (bk, bv) : mergeAssocLists a bs
+      Just av -> (bk, av) : mergeAssocLists (filter (\(x, _) -> x /= bk) a) bs
 
 toYamlObject :: IsYamlScalar k
              => IsYamlScalar v
diff --git a/Setup.hs b/Setup.hs
deleted file mode 100644
--- a/Setup.hs
+++ /dev/null
@@ -1,2 +0,0 @@
-import Distribution.Simple
-main = defaultMain
diff --git a/Setup.lhs b/Setup.lhs
new file mode 100644
--- /dev/null
+++ b/Setup.lhs
@@ -0,0 +1,11 @@
+#!/usr/bin/env runhaskell
+
+> module Main where
+> import Distribution.Simple
+> import System.Cmd (system)
+
+> main :: IO ()
+> main = defaultMainWithHooks (simpleUserHooks { runTests = runTests' })
+
+> runTests' :: a -> b -> c -> d -> IO ()
+> runTests' _ _ _ _ = system "runhaskell -DTEST runtests.hs" >> return ()
diff --git a/data-object-yaml.cabal b/data-object-yaml.cabal
--- a/data-object-yaml.cabal
+++ b/data-object-yaml.cabal
@@ -1,5 +1,5 @@
 name:            data-object-yaml
-version:         0.3.3.1
+version:         0.3.3.2
 license:         BSD3
 license-file:    LICENSE
 author:          Michael Snoyman <michael@snoyman.com>, Anton Ageev <antage@gmail.com>
diff --git a/runtests.hs b/runtests.hs
--- a/runtests.hs
+++ b/runtests.hs
@@ -54,6 +54,7 @@
     , testCase "encode/decode file" caseEncodeDecodeFile
     , testCase "encode/decode strings" caseEncodeDecodeStrings
     , testCase "decode invalid file" caseDecodeInvalid
+    , testCase "encode/decode in order" caseInOrder
     ]
 
 caseEncodeDecode :: Assertion
@@ -159,3 +160,18 @@
     mappingKey res "k1" @?= Scalar (mkStrScalar "1")
     mappingKey res "k2" @?= Scalar (mkStrScalar "2")
     mappingKey res "k3" @?= Scalar (mkStrScalar "4")
+
+inOrderData :: String
+inOrderData = "'Fatal': 'Unknown variable \"bar\"'\n'Date': '2001-11-23 15:03:17 -5'\n'User': 'ed'\n'Stack':\n- 'line': '23'\n  'file': 'TopClass.py'\n  'code': 'x = MoreObject(\"345\\n\")\n\n'\n- 'line': '58'\n  'file': 'MoreClass.py'\n  'code': 'foo = bar'\n"
+
+inOrderData2 :: String
+inOrderData2 =
+       "'a': '1'\n'b': '2'\n'd': '4'\n'c': '3'\n"
+    ++ "'g': '1'\n'n': '2'\n'q': '4'\n'f': '3'\n"
+    ++ "'z': '1'\n'y': '2'\n'x': '4'\n'w': '3'\n"
+
+caseInOrder :: Assertion
+caseInOrder = do
+    Just (Mapping ((x, _):_)) <- return $ decodeYaml inOrderData
+    x @?= mkScalar "Fatal"
+    fmap (B8.unpack . encode) (decodeYaml inOrderData2) @?= Just inOrderData2
