refactored prepare-formula