Various cleanup, factoring out duplicate code

I'll add more of these as I find them, but I wanted to give you a chance to look over these three now. I have tested the code, and it still works the same.