Looking at this code thinking about the coming client / server split made me realise that we could start by at least moving this functionality to a more encapsulated place.