Compressed the many conditionals into a few simple math operations