Query debugging using a helper method

Reduces duplicated code