Improved index searching. Minor refactoring.