finished algorithm for parsering tertiary contacts