refactor of magic.py