Some refactoring.
Added exit after the end of search.