Improved old code to be more modular.