refactored search code