Refactored calcIndex and added comments