Ok, after you merge this expiry logic then I'll start a new PR with some general cleanups (removing the use of cd, and a few other things can be simplified i think).