refactored search method, much cleaner now