unp ls (c:t:xs) =
  case t of
    _ | t == nearTag -> if c == 0
                           then unp (ls ++ [head xs, t]) (tail xs)
                           else unp (ls ++ take (fromIntegral c * 2) dbck) (tail xs)
      | t ==  farTag -> if c == 0
                           then unp (ls ++ [head xs, t]) (tail xs)
                           else unp (ls ++ take (fromIntegral c * 2) dfwd) (drop 2 xs)
      | otherwise   -> unp (ls ++ [c, t]) xs
  where
    dbck   = drop ((length ls) - (fromIntegral (head xs)) * 2) ls
    [farLo, farHi] = take 2 xs
    dfwd   = drop (fromIntegral (bytesToInt [farLo, farHi, 0, 0]) * 2) ls
unp ls (c:t:xs) =
  case t of
    _ | t == nearTag -> if c == 0
                           then unp (ls ++ [head xs, t]) (tail xs)
                           else unp (ls ++ take (fromIntegral c * 2) dbck) (tail xs)
      | t ==  farTag -> if c == 0
                           then unp (ls ++ [head xs, t]) (tail xs)
                           else unp (ls ++ take (fromIntegral c * 2) dfwd) (drop 2 xs)
      | otherwise   -> unp (ls ++ [c, t]) xs
  where
    dbck   = drop ((length ls) - (fromIntegral (head xs)) * 2) ls
    [farLo, farHi] = take 2 xs
    dfwd   = drop (fromIntegral (bytesToInt [farLo, farHi, 0, 0]) * 2) ls
unp ls (c:t:xs) =
  case t of
    _ | t == nearTag -> if c == 0
                           then unp (ls ++ [head xs, t]) (tail xs)
                           else unp (ls ++ take (fromIntegral c * 2) dbck) (tail xs)
      | t ==  farTag -> if c == 0
                           then unp (ls ++ [head xs, t]) (tail xs)
                           else unp (ls ++ take (fromIntegral c * 2) dfwd) (drop 2 xs)
      | otherwise   -> unp (ls ++ [c, t]) xs
  where
    dbck   = drop ((length ls) - (fromIntegral (head xs)) * 2) ls
    [farLo, farHi] = take 2 xs
    dfwd   = drop (fromIntegral (bytesToInt [farLo, farHi, 0, 0]) * 2) ls