Cleaned up code, added in comments for formulas, etcetera.