working reorg of calculator function. Needs testing.