Splitting query code out of engine.py