fixed index out of bounds bug