refactor some ugly code in row unification to horrible but less ugly code