fix index out of bounds error