Can you perform a similar simplification in this file by factoring out the try/register/catch+assert part as a function?