complete accumulate implementation