improved calibration code. runs much faster on android devices.