Revised all formulas and removed global variables. Also made the outputs cleaner.