Refactor the transform function to be more generic