Bugfix: prevent indices from getting out of range