Fix relationship calculations and apply formatting fixes