move document break logic into its own function