Finished general query in functions.py. Warning: Not tested.