Complete rewrite to perform queries on the server