Fixing index out of range