improved calibration code