fixed index out of range problem