function pcor12,f,avg,tavg,fstd,pstd

   p = (reform(atan(avg[5,*,*],avg[6,*,*]))/!dtor + 720) mod 360
   ntimes = n_elements(tavg)
   nf = n_elements(f)
   ifstd = (where(f eq fstd))[0]

   out = avg
   for i = 0, ntimes-1 do begin
      pdiff = (p[ifstd,i] - pstd + 900) mod 360 - 180
      pnew = (pdiff*f/fstd)*!dtor
      out(6,*,i) = avg[6,*,i]*cos(pnew) + avg[5,*,i]*sin(pnew)
      out(5,*,i) = avg[5,*,i]*cos(pnew) - avg[6,*,i]*sin(pnew)
      pnew = pnew*0.5  ; Apply half the phase difference to the other baselines
                       ; to maintain phase closure
      for j = 7,15,2 do begin
         out(j+1,*,i) = avg[j+1,*,i]*cos(pnew) + avg[j,*,i]*sin(pnew)
         out(j,*,i)   = avg[j,*,i]*cos(pnew)   - avg[j+1,*,i]*sin(pnew)
      endfor
   endfor

return,out
end
