Complete rewrite of calc.py