small improvement to closing function