Clean up the new inference algorithm and fix the substituion merge function that was lacking a recursive call.