Improve query preparation method