These 30 line if-else stacks can be made into a min() and a max(), you can cut out a lot of this by doing it in a helper function and calling that.