Minor refactoring and template change.
Fixed a bad bug with table names in sql queries.

I wasn't quoting them with `, so if you had a table that was
also a builtin name, it would fail.