refactoring based on Eric's code review.  Improved search