Refactor and fix calculate function body.

All tests are now passing.