Ongoing code review

I'll push commits as I go.