Moved query search into it's own function