calibration function (more or less done)

also fixed index bugs (thanks to Emily)