Cometary impacts, molecular clouds, and the motion of the Sun perpendicular to the galactic plane