fixed a potential index error