Rewrite calibration script with state-machine