Lots of changes, see comments.