Add some checks for index bounds.