Modified getLoopIndices to handle nest loops

getLoopIndices is made recursive to handle nested loops.