Refactor transform (almost beautiful