Comments and more improvement.

more logic to not do extra processing. A lot more comments to explain whats happening.
pre code style, wrap all conditions in braces, even if one line (less error prone)