Is there an easier way to do all of this?

And when you get bored of debugging that, use a multiplication instead of a division