refactored for optimum database saveage