Fix for index out of range issues