Refactored query manager.