Move close callback to a function