refactor magic