I suspect this bit could be done in python.