Refactor case-merging and identical-alternative optimisations