Query optimizations and cross site attack fix