factored out some code duplication for setting relationships
fixed error