Moved read code into its own function.