Optimized checking for unsaved changes