indeed timo is correct – this has been done for you already in the gen~ examples folder.
however, only ‘direct form 1’ (like MSP). i made this patch a while ago for my own sanity (attached .maxpat). hope it helps. all working and correct as far as i can tell.
by the way, i noticed your clear mechanism. to ‘clear’ the gen~ versions, you need only send ‘0’ to all the history objects inside the gen~, by naming all the history objects and then sending the message via a message box to the gen~ object itself. much easier.