made code more readable, fixed logic bug in assigning relationships