What is the version of the following sentence with correct punctuation?

Thus the idea behind PERZA was that when given access to training and small loans rural residents would be able to start businesses and generate income which they would then be able to spend on energy
Thus the idea behind PERZA was that, when given access to training and small loans, rural residents would be able to start businesses and generate income, which they would then be able to spend on energy.