fixing index out of bounds error