Having trouble formally proving termination