refactoring close button codes