Moved the scoping stuff into search.js and refactored searching.