Commented and slightly refactored all of the code.