Add on-the-spot calibration
Clean up code