fix possible index out of bounds exception