I agree that it's harder to read than I would like, but all the variable sorting stuff at the top is the same either way, and I didn't want to repeat that. But maybe there's a better way to do it that makes it more readable? (One function with sub-functions?)