added in error checking, reformat code in query.py