Refactor common code to loop over variable names.