diff --git a/lib/Text/InvisiXML.hs b/lib/Text/InvisiXML.hs
index f1076c087ba0c3de051ca92d038939b1c520984e..8ba68c3cb6f31c4d6fd7c61939d6f6da1f17b625 100644
--- a/lib/Text/InvisiXML.hs
+++ b/lib/Text/InvisiXML.hs
@@ -128,17 +128,16 @@ restore (context, subStructure) = state $ \parsingState ->
   (at parsingState, parsingState {context, subStructure})
 
 checkout :: MonadError XMLError m => Context -> Parser m ()
-checkout actual = do
-  expected <- gets context
-  case (expected, actual) of
-    (Nothing, Just (TagOccurrence {tag, openLine})) ->
+checkout actual = gets context >>= compareWith actual
+  where
+    compareWith (Just (TagOccurrence {tag, openLine})) Nothing =
       throwError . ClosingUnopen tag $ Line openLine
-    (Just (TagOccurrence {tag, openLine}), Nothing) ->
+    compareWith Nothing (Just (TagOccurrence {tag, openLine})) =
       throwError . Unclosed tag $ Line openLine
-    (Just tO0, Just tO1)
-      | tag tO0 /= tag tO1 ->
-          throwError $ ClosingDoesntMatch (tag tO0) (tag tO1) (Line $ openLine tO1)
-    _ -> return ()
+    compareWith (Just tO0) (Just tO1)
+      | tag tO0 /= tag tO1 =
+          throwError $ ClosingDoesntMatch (tag tO0) (tag tO1) (Line $ openLine tO0)
+    compareWith _ _ = return ()
 
 parse :: (XmlSource s, MonadError XMLError m) => s -> m InvisiXML
 parse = fmap collectState . execStateT fillStructure . openStream . tokens