I’ve also posted this code on the reddit Anki community. It might be worth checking if someone came up with amendments.