I'm not convinced it's large enough that commoning up would make the code significantly faster or any clearer. But I can have a go if you insist