Modified the calculator code to be flexible and reusable

Or at least easier to reason about each individual piece.