Added query validation

Queries are now validated before running them. There is more to be done on this, but I wanted to get this in now, considering it changes the query flow signficantly.