Digital distortion in first delay plugin

Hello, I’m a new time user to Juce trying to make my first delay plugin. I am getting a delay although all output from the delay buffer sounds horribly metallic (I’m assuming some sort of digital distortion?). Am I going along the right lines with this code and why would it be distorting?

for (int i = totalNumInputChannels; i < totalNumOutputChannels; ++i)
    buffer.clear (i, 0, buffer.getNumSamples());

for (int channel = 0; channel < totalNumInputChannels; ++channel)
{
    float* channelData = buffer.getWritePointer (channel);
    float* delayData = delayBuffer->getWritePointer (channel);
    
    for (int i=0; i<buffer.getNumSamples(); i++)
    {
        int delayWrite;
        int sampleDelay = 2000;
        if (delayPosition + sampleDelay < delayBuffer->getNumSamples())
        {
            delayWrite = delayPosition + sampleDelay;
        } else
        {
            delayWrite = (delayPosition + sampleDelay ) - delayBuffer->getNumSamples();
        }
        
        delayData[delayWrite] = channelData[i];
        channelData[i] = delayData[delayPosition] + channelData[i] / 2;
        if (++delayPosition == delayBuffer->getNumSamples())
            {
                delayPosition = 0;
            }
    }
}

Maybe easiest to debug if you set delayBuffer to 4 samples long, sampleDelay to 2 and step through it?

Thanks, it turned out I had feedback as the delay was output as soon as it was written.