Updated version of engine.py