Refactor common code to a loop over variable names.