refactor calibration code