Refactoring and optimization of formulas