Refactor relationship handling code

Fixes #1919 #1814 #2057 #2191 #2177 #2125 #2100 #1848 #1790 #1532 #1389 #1308