Refactor and cleanup mod_calc