refactor duplication in data transformation functions