Refactoring and optimizations of search