diff --git a/edosl0util/merge.py b/edosl0util/merge.py
index 27afc535dad004fcf204e2bde32f6dbb50a603b1..6096b1852a6753bd40137f2d0906be1de52e0176 100644
--- a/edosl0util/merge.py
+++ b/edosl0util/merge.py
@@ -10,8 +10,7 @@
 """
 import logging
 import os
-from collections import deque, OrderedDict
-from functools import total_ordering
+from collections import deque
 
 from . import eos as _eos
 
@@ -71,17 +70,18 @@ class _Ptr(object):
 def _sort_key(p, order=None, eos=False):
     if eos and p.apid == 64:
         return _eos._modis_sort_key(p)
-    return (p.stamp, order.index(p.apid) if order else p.apid)
+    apid_order = order.index(p.apid) if p.apid in order else 0
+    return (p.stamp, apid_order if order else p.apid)
 
 
 def read_packet_index(stream, order=None, eos=False):
+    order = tuple(order) if order else tuple()
     index = deque()
     try:
         # drop any leading hanging packets
         count = 0
         packet = stream.next()
         while not packet.stamp:
-        #while not (packet.is_first() or packet.is_standalone()):
             packet = stream.next()
             count += 1
         if count:
@@ -89,7 +89,6 @@ def read_packet_index(stream, order=None, eos=False):
 
         while True:
             if not packet.stamp:
-            #if not (packet.is_first() or packet.is_standalone()):
                 # corrupt packet groups can cause apid mismatch
                 # so skip until we get to the next group
                 packet = stream.next()
@@ -107,7 +106,6 @@ def read_packet_index(stream, order=None, eos=False):
             # collect all packets for this timecode/group
             packet = stream.next()
             while not packet.stamp:
-            #while not (packet.is_first() or packet.is_standalone()):
                 # Bail if we're collecting group packets and apids don't match
                 # This means group is corrupt
                 if ptr.apid != packet.apid: