in general, this is a bit of callback hell and a bit rough to read. Can we save everything that happens on close for step 1 into a separate function, defined somewhere elsewhere in the file?