refactoring and bug fix to searches