Added some code, which is as yet untested.