Morse theory for Hamiltonians