much nicer implementation of union