Refactoring to break out the template application into its own function.