refactoring search code