Huge commit after git-mess

Lots of stuff re-written.