Refactorization and calibration code