Add calibration and fix write to file bugs