refactored search query handling