moved much of the logic into a class to clean up the index file quite a bit.