Search: bug fixes and refactoring