Lots of code added, mostly untested.