Refactor open/save logic and close event