Some code cleanup/reorganizing for adv search