Faster relationships

Rebased version of #269