Begin complete rewrite of the db query code