cstub: refactor to reduce repetition