Implement the main search function.