Rewrote comments and code re-ordering.