corrected hook to save state