Bugfix: Prevent index from going out of range.