Move more code in, to be called from elsewhere.