Move queries logic into app.py