Refactored unification algorithm