Fixed accumulation of index bug (credit to: wkurniawan07)