Refactoring handleMaximize() function