Refactor, add comments and restyle.

I switched to K&R style with my linked if/else  blocks. I think it looks
better that way. I added some comments to better explain what is
happening.