finished magic.py and corresponding tests