move lots of code out of main.c, finish calibration routine