Split run_query into two functions for simplicity