Refactor connection and device relationship