Fixed event handler for page leave