Commented and fixed half the code. Just committing to be sure nothing goes awry.