Some code cleanup and addition of some comments to help the reader understand what is going on!