Some cleanup and rearrangement of things

See individual commits for details. Note in particular the tabs to spaces conversion; this is something many people argue about for different reasons, but if you think it's the right way to go, you'll need to tweak your editor so that future code changes use spaces too.

(If you're wondering where all this comes from, there's a text-linking feature I want to work on, but I haven't had much time recently - here's the work I did in preparation for it)