Preliminary code, untested.