Fixed prior error

I made huge error in a prior commit and have replaced the code. I also
am including inline comments with the code for further reference.