Refactored a little, somewhat modularized the code a little.