Code review

I've made a lot of suggested changes that we should go through together. I think there's more we can do, but let's start with this, it's already pretty big. If you want to go over these alone, let me know and I can comment them better.