improve list index out of range avoiding