Refactored a bit and added saving on close