Refactor searching to a class and integrate with error handling