Various improvments, cleaning and optimization of the calibration code.