Code for collaboration with Timothy Barraclough.