bug fixed for index out of range