Improve union handling

Related to #842