refactored, rearranged code to work with mega indexes