Refactoring
Some cleanups
More refactoring
Nice use of min() to kill an if statement