Moved extraction code into its own function.