They shared the code, but then, I found key errors in their work. How do I criticize this in my research paper?
I would think several times before using the word 'corrected', especially if I don't fully understand the code, as it seems from your question ("...I studied the code and was able to decipher the meanings of various parts of the code."). Understanding various parts is different from understanding it entirely, the latter being a prerequisite to improving upon it or correcting it.

However, if you are sure that mistakes exist in the code, you have two options:

(a) Point out those mistakes and share them with X and Y before trying to publish it. Once you publish, the credibility of their code goes down - if some parts are wrong others could be too. Therefore it is only fair that you convey the errors to them first. You aren't obligated to, but I think it's the right thing to do.

(b) If you're significantly extending the code, write your own-   it could be based on their code, and you can acknowledge that, but by writing your own code you take full responsibility for it, and are not propogating any errors inherent to the original code. In terms of quantum of work, I think this is more significant, and gives you more room to develop it for your particular purpose.