refactored most code out of index