Copy popup-closing code all over the place

TODO: tidy it up
Hmm shuffling fields, not sure why