modify safesum function