Refactor relationships processing