Factor serializing into its own function. Again, less stuff in main().