YIN Pitch detection Algortithm ( how do I improve my results )
If you are not doing this in low-latency real-time, you can work backwards from the stable portion of the pitch estimate to the transient attack portion of the waveform at the beginning.

The sound of a plucked guitar string evolves in a possibly predictable pattern over time (e.g. more so than voice).  If you can estimate the onset time and/or have neighboring pitch estimates, you can adaptively set window sizes and threshold levels over time to more optimal values, as potentially determined by experimentation on some data set of guitar notes.  You can also use statistical decision theory to determine if any local pitch estimate fits the history of a reasonable spectral evolution of any guitar note, and reject outliers as noise, transients, harmonics or octave errors (harmonic and octave errors potentially being correctable errors).  This is especially useful working backwards in time, as the attack is usually noisier than the sustain/decay portion of a note's evolution.

Some improvement in steps 4) and 6) can also be acquired from psycho-acoustic experimentation with human listeners.  For instance: For an octave difference to be perceived above a certain error rate by typical human listeners, how much normalized difference in octave pitch estimate peaks over what amount of time is required?  Any difference over a smaller amount of time might be imperceptible.

ADDED: A window size of 1024 at a sample rate of 44100 only allows correlating/using a little more than 2 periods of the pitch of the lowest string of a guitar (E4 = 82.40Hz).  A 3X or 4X longer window might be more reliable for the lowest guitar notes, but shorter windows would be more responsive or provide better time locality for the higher fretted notes.