changed format of code to take advantage of the automatic index