I kinda wonder if the cancel_timeout function should operate on and return an updated State#state{}.

This would eliminate whole parts of the function-head #state{} pattern and the Tref bits both here and in other parts of the code.