proper closing on close event