Refactored cumsum