Possibly fixed bug with index out of range (haven't tested yet).