Break this out into its own top-level function