US stocks shake off early losses to end higher