move calibrate code into function