improved the unify function