How Gravity Can Explain the Collapse of the Wavefunction