Fixed quoting for subqueries, added unit tests.