fix uninitialized memory; generalize diff function in monotonefw