refactor of calculation code