Small refactoring of the closing functionality