changed all indices to pre-increment when possible