Fix bounds check bug