put comments and improve testing on index variable