Major rewrite. Lots of changes.