Fixes #461. Also fixes the relation ordering to go by type first and then direction.