Refactoring and simplification of query handling.