Fixed some index out of bounds bugs.