Initial working (I think) version.