rewrote much of the join function