Implemented structured exceptions

Removed all usages of exit and replaced them with exceptions. I also
added a tiny bit of formatting in a few places and changed a bit of
Boolean logic.

The relative error tolerance is also now the inverse of the epsilon,
which is slightly smaller than what it was before.