typ: refactor to reduce code duplication