I have added some comments

I have added comments and changed the conditions to fit for a good
result.