Refactor some common code for data formatting.