Fix out-of-bounds check