Search fix and refactoring