nearly completed except for cutoff checking