fixed an index out of range bug