custom formulas and overall code refactoring